我有一个拥有另一个实体的实体
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
第二个说:
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.
是否可以在不手动修改迁移的情况下实现?
答案 0 :(得分:2)
显然EF Core还不支持此功能。
在GitHub上查看此问题: https://github.com/aspnet/EntityFrameworkCore/issues/11336
还提供了一种变通方法,我尚未进行自我测试。