7

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

7
  • I'm assuming you're using the code-first approach? Commented Feb 26, 2015 at 11:02
  • Yes, this is code first approach. I am following from this article Commented Feb 26, 2015 at 11:04
  • Have you seen this stackoverflow.com/questions/18907411/…? Commented Feb 26, 2015 at 11:18
  • You need the Required attribute on Id in your AspNetRoles class. Commented Feb 26, 2015 at 11:18
  • Can you show us your POCO class too? Commented Feb 26, 2015 at 11:19

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.