Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueObject not stored / useable #9835

Closed
puschie286 opened this issue Aug 17, 2021 · 4 comments
Closed

ValueObject not stored / useable #9835

puschie286 opened this issue Aug 17, 2021 · 4 comments
Labels

Comments

@puschie286
Copy link

puschie286 commented Aug 17, 2021

ValueObjects seems to be not stored by default and [Owned] doesnt work when using ValueObjects with abp. ( Microsoft Source )

You can force ef to store them but it will ignore futher configuration ( like IsRequired ) and it requires you to always define the column name for ValueObjects with multiple properties

// dbcontext configuration
builder.Entity<Request>( e =>
{
	// valueobject with one property
	e.OwnsOne( x => x.Time );
	
	// valueobject with multiple properties
	e.OwnsOne( x => x.Time, n =>
    {
	n.Property( x => x.Start ).HasColumnName( "Request_Start" );
        n.Property( x => x.End ).HasColumnName( "Request_End" );
    } );
}
// entity
public class Timerange: ValueObject
{
	public DateTime Start { get; }
	public DateTime End { get; }

	private Timerange() {}

	public Timerange( DateTime start, DateTime end )
	{
		Start = start;
		End = end;
	}

	protected override IEnumerable<object> GetAtomicValues()
	{
		yield return Start;
		yield return End;
	}
}

checked all examples but you dont use ValueObjects at all so im a bit curios if it works at all ?
Would be great to see an example about how to use ValueObjects and specific as primary ( to prevent using plain GUID and assign a GUID from a different entity )

ABP Framework version: 4.4
Database provider: EF Core

@puschie286
Copy link
Author

Found a workaround for now ( Source

Define navigation after OwnsOne for additional configuration

// single propterty valueobject
e.OwnsOne( x => x.Optionen );
e.Navigation( x => x.Optionen ).IsRequired();
// multiple property valueobject
e.OwnsOne( x => x.Zeit, b =>
{
    b.Property( x => x.Start ).HasColumnName( "Zeit_Start" );
    b.Property( x => x.End ).HasColumnName( "Zeit_End" );
});
e.Navigation( x => x.Zeit ).IsRequired(); // not possible to configure specific for each property

still prefer to use annotation for this configuration to DRY for every entity that use value objects ( so almost all )

@stale
Copy link

stale bot commented Oct 16, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the inactive label Oct 16, 2021
@puschie286
Copy link
Author

push

@stale stale bot removed the inactive label Oct 17, 2021
@stale
Copy link

stale bot commented Dec 16, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the inactive label Dec 16, 2021
@stale stale bot closed this as completed Dec 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant