Is there a data annotation for unique constraint in EF Core (code first)?
问题 I am wondering if there is a data annotation for unique constraint in Entity Framework Core 2 code first approach? 回答1: In EF Core You could use the extension method HasAlternateKey in fluent API only. There are no data annotations to realize a unique constraint . This MS doc article - Alternate Keys (Unique Constraints) - will explain how to use and which further possibilities are exist. A short example from link above: class MyContext : DbContext { public DbSet<Car> Cars { get; set; }