I need to create a unique constraint on multiple fields, and those fields are ValueObjects.
Let's say I have this
public class MyEntity
{
public EntityCode Code {get;set;}
public SecondaryCode Second {get;set;}
}
public class EntityCode : ValueObject<string>
{
public string Value {get;set;}
public string Description {get;set;}
}
public class SecondaryCode: ValueObject<string>
{
public string Value {get;set;}
public string Description {get;set;}
}
I can create a unique constraint like this
public class MyEntityConfiguration : EntityTypeConfiguration<MyEntity>
{
...
builder.OwnsOne(p => p.Code)
.HasIndex(p => p.Value)
.IsUnique()
...
}
But I would like a composite key and I can't figure out how to do this with value object. Take a look at this sample, it is not working as I expected
builder.OwnsOne(p => p.Code);
builder.OwnsOne(p => p.Second);
builder
.HasIndex(p => new { p.Code.Value, p.Seconde.Value })
.IsUnique()
Any help would be appreciated