我正在Asp中迁移一个代码优先的域(初始创建(。Net Core 2.1 web应用程序。
add migration命令创建了以下代码(为简洁起见,删除了无错误的部分(
migrationBuilder.CreateIndex(
name: "RoleNameIndex",
schema: "BatlCtx",
table: "AspNetRoles",
column: "NormalizedName",
unique: true,
filter: "[NormalizedName] IS **NOT** NULL");
migrationBuilder.CreateIndex(
name: "UserNameIndex",
schema: "BatlCtx",
table: "AspNetUsers",
column: "NormalizedUserName",
unique: true,
filter: "[NormalizedUserName] IS **NOT** NULL");
以下是表创建的迁移代码
migrationBuilder.CreateTable(
name: "AspNetUsers",
schema: "BatlCtx",
columns: table => new
{
Id = table.Column<string>(nullable: false),
UserName = 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),
FirstName = table.Column<string>(maxLength: 50, nullable: false),
LastName = table.Column<string>(maxLength: 80, nullable: false),
Gender = table.Column<int>(nullable: false),
MobilePhone = table.Column<string>(maxLength: 24, nullable: false),
DateOfBirth = table.Column<DateTime>(nullable: false),
UserPicture = table.Column<byte[]>(nullable: true),
TimesLoggedIn = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUsers", x => x.Id);
});
当第一次尝试更新数据库时,出现此错误
系统。数据SqlClient。SqlException(0x80131904(:关键字"NOT"附近的语法不正确。在系统中。数据SqlClient。SqlConnection。OnError(SqlException异常,布尔breakConnection,操作
1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction(在系统中。数据SqlClient。TdsParser。ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔调用程序HasConnectionLock,布尔异步关闭(注意:我删除了代码中的"filter:"[NormalizedName]IS NOT NULL"部分,但我仍然得到相同的错误
CreateIndex方法中粗体的NOT关键字(帖子中的第一个代码片段(是罪魁祸首。现在,如果我重写了那个代码,我可以理解,但这是从添加迁移中生成的代码。我不明白为什么这是错误的。
请浏览此Post
您可以删除筛选器并运行更新数据库。如下所示:
migrationBuilder.CreateIndex(
name: "RoleNameIndex",
schema: "BatlCtx",
table: "AspNetRoles",
column: "NormalizedName",
unique: true);