我在Migration Builder上收到错误消息,即一列的长度或比例与引用列的长度或比例不同。
上面是我尝试创建迁移构建器的表,它抱怨UserId。
这是我的两个表的迁移构建器代码。
migrationBuilder.CreateTable(
name: "AspNetUsers",
columns: table => new
{
Id = table.Column<string>(nullable: false),
UserName = table.Column<string>(maxLength: 256, nullable: true),
FirstName = table.Column<string>(maxLength: 256, nullable: true),
LastName = table.Column<string>(maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
Email = table.Column<string>(maxLength: 256, nullable: true),
NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
PasswordHash = table.Column<string>(nullable: true),
SecurityStamp = table.Column<string>(nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true),
PhoneNumber = table.Column<string>(nullable: true),
PhoneNumberConfirmed = table.Column<bool>(nullable: false),
TwoFactorEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
LockoutEnabled = table.Column<bool>(nullable: false),
AccessFailedCount = table.Column<int>(nullable: false),
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUsers", x => x.Id);
});
这是aspnet用户表,然后是组织表
migrationBuilder.CreateTable(
name: "Organization",
columns: table => new
{
OrganizationId = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1,1"),
DistrictId = table.Column<int>(nullable: true),
Name = table.Column<string>(nullable: true),
Address1 = table.Column<string>(nullable: true),
Address2 = table.Column<string>(nullable: true),
City = table.Column<string>(nullable: true),
State = table.Column<string>(nullable: true),
Zip = table.Column<string>(nullable: true),
Country = table.Column<string>(nullable: true),
County = table.Column<string>(nullable: true),
Latitude = table.Column<string>(nullable: true),
Longitude = table.Column<string>(nullable: true),
TimeZone = table.Column<string>(nullable: true),
UserId = table.Column<string>(nullable: true),
OrganizationLogo = table.Column<string>(nullable: true),
SubscriptionId = table.Column<int>(nullable: true),
DateAdded = table.Column<DateTime>(nullable: true),
DateDeleted = table.Column<DateTime>(nullable: true),
DateUpdated = table.Column<DateTime>(nullable: true),
UpdatedBy = table.Column<string>(nullable: true),
AddedBy = table.Column<string>(nullable: true),
DeletedBy = table.Column<string>(nullable: true),
},
constraints: table =>
{
table.PrimaryKey("PK_Organization", x => new {x.OrganizationId });
table.ForeignKey(
name: "FK_Organization_District_DistrictId",
column: x => x.DistrictId,
principalTable: "District",
principalColumn: "DistrictId",
onDelete: ReferentialAction.Cascade
);
table.ForeignKey(
name: "FK_Organization_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade
);
table.ForeignKey(
name: "FK_Organization_Subscription_SubscriptionId",
column: x => x.SubscriptionId,
principalTable: "Subscription",
principalColumn: "SubscriptionId",
onDelete: ReferentialAction.Cascade
);
});
我尝试在组织表上将userid nullable设置为false,这没有帮助。我不确定在这里做什么以建立我想要的关系。
可能是因为创建的列具有不同的nvarchar长度。尝试将迁移中的PK / FK列的maxLength指定为450。
是的,这是正确的,即使它没有提供最大长度的身份框架也在幕后进行,我不得不弄清楚