c# - .NET:如何使用 Entity Framework Core 3.1 Migrations 的 CreateTableBuilder 定义唯一约束?
问题描述
我正在使用Entity Framework Core 3.1 Migrations创建一个 SQL Server 表并使用MigrationBuilder
和CreateTableBuilder
类,如下所示:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Example",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
Foo = table.Column<string>(maxLength: 256, nullable: false),
Bar = table.Column<string>(maxLength: 256, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Example", x => x.Id);
});
}
我已经能够像这样在单个列上定义一个 UNIQUE 约束:
table.UniqueConstraint(
name: "Unique_Foo",
columns: x => x.Foo);
但是我需要在两列(例如:Foo
和Bar
)上定义一个 UNIQUE 约束,并且我无法在columns
参数中表达这一点。(参数的类型是System.Linq.Expressions.Expression<Func<TColumns,object>>
)
如何使用CreateTableBuilder
该类在两个或更多列上定义 UNIQUE 约束?
解决方案
尝试这个:
columns: x => new {x.Foo, x.Bar}
推荐阅读
- mysql - 您可以在 CouchDB 上创建/存储/维护关系数据吗?
- dynamic - SAILSJS 的动态模型属性
- javascript - 基于从另一个数据集计算的范围的颜色条形图
- javascript - 如何在 trianglify.min.js 中使用单一颜色?
- excel - 如何在所有工作表中保持页眉(不是静态页眉)相同?
- postgresql - 使用scala将变量从Spark传递给Postgresql
- elasticsearch - 在 GKE 上从 filebeat 接收日志到 ECK
- android - MVVM 平均 Fragment 膨胀时间以及如何提高性能
- sql-server - 有没有办法使用 cloudformation 从 S3 恢复 RDS 中的 SQL Server 备份?
- powershell - 如何在 GitLab CI YAML 文件中包含 PowerShell 脚本文件