我找不到方法添加一个唯一的约束我的字段使用属性:

public class User
{
    [Required]
    public int Id { get; set; }

    [Required]
    // [Index("IX_FirstAndSecond", 2, IsUnique = true)] not supported by core
    public string Email { get; set; }

    [Required]
    public string Password { get; set; }
}

我正在使用这些包:

 "Microsoft.EntityFrameworkCore": "1.0.1",
 "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
 "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.1",
 "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",

当前回答

在。net Core 2.2中,这些方法都不适合我,但我可以调整一些代码,定义一个不同的主键来实现这个目的。

在下面的例子中,我想确保OutletRef字段是唯一的:

public class ApplicationDbContext : IdentityDbContext
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Outlet>()
                .HasIndex(o => new { o.OutletRef });
        }
    }

这将在数据库中添加所需的惟一索引。 但是它没有提供指定自定义错误消息的能力。

其他回答

我们可以使用fluent api添加唯一键索引。下面的代码对我有用

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<User>().Property(p => p.Email).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_EmailIndex") { IsUnique = true }));

    }

对于那些尝试了所有这些方法但都不起作用的人来说,试试这个,它对我很有效

protected override void OnModelCreating(ModelBuilder builder)
{

    builder.Entity<User>().Property(t => t.Email).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_EmailIndex") { IsUnique = true }));

}

Ef核心支持独特配置。

protected override void OnModelCreating(ModelBuilder builder)
{
  builder.Entity<Account>()
    .HasIndex(account => account.Email)
      .IsUnique();
}

Ef核心支持多个唯一键

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.Entity<Account>()
      .HasKey(account => new { account.Id, account.Email, account.RoleId });
}

不要忘记执行ef core命令进行迁移和更新数据库

>> dotnet ef migrations add MigrationName -c YourDbContextName
>> dotnet ef database update -c YourDbContextName

另外,如果你想在多个列上创建唯一的约束,你可以简单地这样做(遵循@Sampath的链接)

class MyContext : DbContext
{
    public DbSet<Person> People { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>()
            .HasIndex(p => new { p.FirstName, p.LastName })
            .IsUnique(true);
    }
}

public class Person
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

自从实体框架核心(EF核心)5.0以来,我们可以通过数据注释配置唯一索引。

它与例如EF6略有不同,因为我们不能在属性本身上设置它,而是在类上设置它。

using Microsoft.EntityFrameworkCore;           // for [Index] attribute
using System.ComponentModel.DataAnnotations;   // for [Key] and [Required] attributes

namespace FunWithDataAnnotations
{
    [Index(nameof(EmailAddress), IsUnique = true)]
    public class User
    {
        [Key]
        public Guid Id { get; set; }

        [Required]
        public string FullName { get; set; }

        [Required]
        public string EmailAddress { get; set; }
    }
}

有关索引和数据注释的更多信息,请参见:https://learn.microsoft.com/en-us/ef/core/modeling/indexes?tabs=data-annotations