[英].NET: How to Define a Unique Constraint using Entity Framework Core 3.1 Migrations' CreateTableBuilder?
I am creating a SQL Server table using Entity Framework Core 3.1 Migrations and utilizing the MigrationBuilder
and CreateTableBuilder
classes, like so:我正在使用Entity Framework Core 3.1 迁移并利用MigrationBuilder
和CreateTableBuilder
类创建 SQL 服务器表,如下所示:
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);
});
}
I've been able to define a UNIQUE constraint on a single column like so:我已经能够像这样在单个列上定义一个 UNIQUE 约束:
table.UniqueConstraint(
name: "Unique_Foo",
columns: x => x.Foo);
But I need to define a UNIQUE constraint on two columns (ex: Foo
and Bar
) and I've not been able to express this in the columns
argument.但是我需要在两列(例如: Foo
和Bar
)上定义一个 UNIQUE 约束,而我无法在columns
参数中表达这一点。 (The type of the argument is System.Linq.Expressions.Expression<Func<TColumns,object>>
) (参数的类型是System.Linq.Expressions.Expression<Func<TColumns,object>>
)
How do I define a UNIQUE constraint on two or more columns using the CreateTableBuilder
class?如何使用CreateTableBuilder
class 在两列或多列上定义唯一约束?
Try this:尝试这个:
columns: x => new {x.Foo, x.Bar}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.