我找不到方法添加一个唯一的约束我的字段使用属性:
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",
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
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