Entity framework core 在EF Core中生成具有所属实体的复合唯一约束/索引
我有一个实体拥有另一个实体Entity framework core 在EF Core中生成具有所属实体的复合唯一约束/索引,entity-framework-core,Entity Framework Core,我有一个实体拥有另一个实体 public class Entity1 { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public virtual int ID { get; set; } public string Property { get; set; } public Entity2 Description { get; set; } } public class Entity2 {
public class Entity1
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual int ID { get; set; }
public string Property { get; set; }
public Entity2 Description { get; set; }
}
public class Entity2
{
public string Test { get; set; }
}
我需要在Entity1.Property和Entity2.Test上创建一个索引。配置如下
builder.OwnsOne(pt => pt.Description);
builder.HasIndex(p => new { p.Property, p.Description.Test }).IsUnique();
//builder.HasIndex("Property", "Description_Test").IsUnique();
我尝试了以上两种代码,但都不起作用。第一个说
The properties expression 'p => new <>f__AnonymousType3`7(Property = p.DeviceClassId,
Test = p.Description.Test)' is not valid. The expression should represent a property
access: 't => t.MyProperty'. When specifying multiple properties use an anonymous type:
't => new { t.MyProperty1, t.MyProperty2 }'.
Parameter name: propertyAccessExpression
这可以在不手动修改迁移的情况下实现吗?显然EF Core还不支持此功能 请参阅GitHub上的此问题: 还有一个变通方法,我自己还没有测试过
The property 'Description_test' cannot be added to the type 'Entity1' because there was no
property type specified and there is no corresponding CLR property or field. To add a
shadow state property the property type must be specified.