简体   繁体   English

如何使用EF为ASP.NET Core定义索引?

[英]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.

相关问题 如何在不使用EF的情况下连接ASP.NET Core Web API中的数据库? - How do I connect to database in ASP.NET Core Web API without using EF? 如何在 ASP.NET Core 中使用自定义验证获取索引处的属性? - How do I get a property at index using custom validation in ASP.NET Core? 如何使用 EF Core 3.1.5 解决 ASP.net Core 应用程序中的 null 引用异常? - How can I resolve null reference exception in ASP.net Core application using EF Core 3.1.5? 如何在使用 EF 的 asp.net core 3.1 MVC 应用程序中正确使用我的 SQLite 数据库? - How do I properly use my SQLite db in my asp.net core 3.1 MVC application using EF? 如何在 EF 和 ASP.NET Core 5 MVC 中创建表? - How I create a table in EF and ASP.NET Core 5 MVC? 如何从 ASP.NET Core 6 MVC EF 中的 controller 中的另一个 controller 实例化服务? - How do I instantiate a service from another controller in a controller in ASP.NET Core 6 MVC EF? 如何使用EF在asp.net core中使用存储过程 - how to use stored procedure in asp.net core using EF 如何使用EF6在ASP.NET WebForms应用程序中使用tagPrefix ef? - How do I make use of the tagPrefix ef in an ASP.NET WebForms app using EF6? 我是否需要在ASP.NET Core EF中为每个外键定义两个字段 - Should I need to define two fields per foreign key in ASP.NET Core EF 如何使用 ASP.NET Core 3.1 MVC 和 EF Core 对唯一字段使用数据验证注释? - How do use data validation annotation for unique fields with ASP.NET Core 3.1 MVC and EF Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM