ASP.NET MVC 6 - Assigning NOT NULL to the Primary Key in Entity Framework 7

asp.net asp.net-core-mvc c# entity-framework-core

Question

Code:

migrationBuilder.AddPrimaryKey("AspNetRoles", "PK_AspNetRoles", new[] { "Id" }, isClustered: true);

I am getting the error message as Cannot define PRIMARY KEY constraint on nullable column in table 'AspNetRoles'.

Namespace of MigrationBuilder - Microsoft.Data.Entity.Relational.Migrations.Builders

How to assign NOT NULL to the above code in ASP.NET MVC 6

Popular Answer

For a very temporary workaround to be able to write some code-first EF entities and advance, I commented out the part of the migration script dealing with those keys.

Obviously this butchers the relations in the ASP.NET users and roles tables and isn't a workable long term solution, I assume it will be fixed or someone will find a real solution at some point.

Some of the lines I commented out (I got the same error with Users once I changed Roles).

        //migrationBuilder.AddPrimaryKey("AspNetRoles", "PK_AspNetRoles", new[] { "Id" }, isClustered: true);

        //migrationBuilder.AddPrimaryKey("AspNetUsers", "PK_AspNetUsers", new[] { "Id" }, isClustered: true);
        ....
        //migrationBuilder.AddForeignKey(
        //    "AspNetRoleClaims",
        //    "FK_AspNetRoleClaims_AspNetRoles_RoleId",
        //    new[] { "RoleId" },
        //    "AspNetRoles",
        //    new[] { "Id" },
        //    cascadeDelete: false);

        //migrationBuilder.AddForeignKey(
        //    "AspNetUserClaims",
        //    "FK_AspNetUserClaims_AspNetUsers_UserId",
        //    new[] { "UserId" },
        //    "AspNetUsers",
        //    new[] { "Id" },
        //    cascadeDelete: false);

        //migrationBuilder.AddForeignKey(
        //    "AspNetUserLogins",
        //    "FK_AspNetUserLogins_AspNetUsers_UserId",
        //    new[] { "UserId" },
        //    "AspNetUsers",
        //    new[] { "Id" },
        //    cascadeDelete: false);
        ...
        //migrationBuilder.DropForeignKey("AspNetRoleClaims", "FK_AspNetRoleClaims_AspNetRoles_RoleId");

        //migrationBuilder.DropPrimaryKey("AspNetRoles", "PK_AspNetRoles");

        //migrationBuilder.AddForeignKey(
        //    "AspNetRoleClaims",
        //    "FK_AspNetRoleClaims_AspNetRoles_RoleId",
        //    new[] { "RoleId" },
        //    "AspNetRoles",
        //    new[] { "Id" },
        //    cascadeDelete: false);

        //migrationBuilder.AddForeignKey(
        //    "AspNetUserLogins",
        //    "FK_AspNetUserLogins_AspNetUsers_UserId",
        //    new[] { "UserId" },
        //    "AspNetUsers",
        //    new[] { "Id" },
        //    cascadeDelete: false);

This is presumably a bug or missing feature in the very early build of EF7?



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why