[英]How do I define an index for ASP.NET Core using EF?
I'm using EF.Core in an ASP.NET core project. 我在ASP.NET核心项目中使用EF.Core。 I have a model class, but can't find any documentation about what attributes to set on a property or the class to create an index or a unique index.
我有一个模型类,但找不到任何有关在属性上设置哪些属性或创建索引或唯一索引的类的文档。 Is this possible?
这可能吗?
This is currently not supported in EF core (at least not yet). EF内核当前不支持此功能(至少目前尚不支持)。 I believe this is in the pipeline to be a standard annotation instead of a separate downloadable package.
我认为这正在成为标准注释,而不是单独的可下载软件包。 You can use the fluent API instead:
您可以改用fluent API:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<User>()
.HasIndex(u => u.EmailAddress).Unique();
}
According to documentation : 根据文件 :
Indexes can not be created using data annotations.
不能使用数据注释创建索引。
But, you can use the Fluent API for this purpose. 但是,您可以为此使用Fluent API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.