问题描述
我有一个实体拥有另一个实体
I have an entity that owns another entity
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 上创建一个索引.配置是这样的
and I need to create an index on Entity1.Property and Entity2.Test. The configuration is like this
builder.OwnsOne(pt => pt.Description);
builder.HasIndex(p => new { p.Property, p.Description.Test }).IsUnique();
//builder.HasIndex("Property", "Description_Test").IsUnique();
我尝试了上述两个代码,但它们不起作用.第一个说
I tried both of the above code but they do not work. The first says
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.
这可以在不手动修改迁移的情况下实现吗?
Can this be achieved without modifying the migration manually?
推荐答案
显然 EF Core 尚不支持此功能.
Apparently EF Core doesn't support this feature yet.
在 GitHub 上查看此问题:https://github.com/aspnet/EntityFrameworkCore/issues/11336
See this issue on GitHub:https://github.com/aspnet/EntityFrameworkCore/issues/11336
还提供了一种解决方法,但我没有亲自测试过.
There is also a workaround offered, which I have not tested myself.
这篇关于在 EF Core 中生成具有拥有实体的复合唯一约束/索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!