asp.net-core - 具有唯一约束的实体框架核心版本 5 问题
问题描述
我在 EF Core 5 (5.0.11) 中遇到了唯一约束的问题。
我创建了一个OnModelCreating
方法UserContext
:
public UserContext(DbContextOptions<UserContext> options) :base(options)
{
}
public DbSet<User> users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasIndex(b => b.Email)
.IsUnique();
}
和或在模型中
[Index(nameof(Email), IsUnique = true)]
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
但是如果我运行迁移,我只有这个(没有唯一约束)
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "users",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(max)", nullable: true),
Email = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_users", x => x.Id);
});
}
解决方案
推荐阅读
- api - Shopware 6 通过 store-api 获取父产品
- python - 用于嵌套字典的 Django 模板中的 For 循环
- angular - 父级的 ngAfterViewInit 与子级的 ngAfterViewInit
- javascript - JavaScript将textarea值复制到剪贴板不起作用
- python - 如何修复 TypeError:+ 不支持的操作数类型:'datetime.time' 和 'datetime.timedelta'
- css - 如何排除 CSS 以影响页面中的滑块?
- python - Flask Scheduler 在视图函数之外运行时失败
- vba - 公式本地 VBA
- python - 使用 lmfit 进行曲线拟合后从拟合统计参数中提取变量
- python - 创建与模型本身的关系