我有两列名为
猴子
和
驴
。两者都不是空的,即。
null
不允许。设置声明如下。
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
...
builder.Entity<Thing>()
.Property(a => a.Monkey)
.IsRequired();
builder.Entity<Thing>()
.Property(a => a.Donkey)
.IsRequired();
}
现在,我了解到组合的内容必须是唯一的,即任何记录都不能有相同的猴子和驴子。根据
MSDN
(多个道具的索引),我应该使用
HasColumnAnnotation
并传递的实例
IndexAnnotation
。中也有建议
this answer
使用
IndexAnnotation.AnnotationName
从…起
System.Data.Entity.Infrastructure.Annotations
.
问题是我找不到那个方法,只能看到
HasAnnotation
。我不清楚这是否是最近的版本(因为链接到的帖子现在有点过时了)。我无法访问命名空间
实体
在中
using
声明,两者都没有。
builder.Entity<Thing>()
.Property(a => a.Monkey)
.HasAnnotation(...);
这是正确的方法吗?还是我应该继续寻找另一种注释方法?缺少什么来获取命名空间?索引是一开始的合适方式吗?
我也尝试过索引,但这需要我声明一个专门的类,这看起来很笨拙,表明这是错误的做法。
builder.Entity<Thing>()
.HasIndex(a => new IndexDedicatedClass(a.Monkey, a.Donkey))
.IsUnique();