NOT keyword error in Db update from SQL migration from Asp.Net Core 2.1 web app

0

I am migrating a code first domain (Initial creation) in an Asp.Net Core 2.1 web application.

The add-migration command created the following code (non erroring parts removed for brevity)

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");

Here is the code from the migration for the table creation

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);
        });

When attempting to update-database for the first time, this error is occurring

System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'NOT'. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) Note: I removed the "filter: "[NormalizedName] IS NOT NULL"" part of the code, but I am still getting the same error

The bolded NOT keywords in the CreateIndex methods (first code snippet in post) are the culprit. Now, I could understand if I WROTE that code, but this is generated code from the add-migration. I don't understand why this is erring.

c#
asp.net-identity
sql-server-2016
asp.net-core-2.1
asked on Stack Overflow Nov 28, 2018 by dinotom • edited Nov 28, 2018 by dinotom

1 Answer

0

Please go through this Post

You may remove the filter and run update-database. Like below:

migrationBuilder.CreateIndex(
    name: "RoleNameIndex",
    schema: "BatlCtx",
    table: "AspNetRoles",
    column: "NormalizedName",
    unique: true);
answered on Stack Overflow Nov 28, 2018 by Sonal Borkar

User contributions licensed under CC BY-SA 3.0