ASP.NET MVC 6 - NOT NULL Assignment for a Primary Key in Entity Framework 7

code:

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

I get an error like Cannot define PRIMARY KEY constraint on nullable column in table 'AspNetRoles'.

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

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

+6
source share
1 answer

For a very temporary workaround, in order to be able to write some EF objects with code first and in advance, I commented on the script migration part related to these keys.

Obviously, this softens relations in the ASP.NET user and role tables and is not a reliable long-term solution, I assume that it will be fixed or someone will find a real solution at some point.

Some lines that I commented on (I got the same error with users as soon as 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); 

Is this apparently a bug or missing feature in the earliest version of EF7?

0
source

Source: https://habr.com/ru/post/983037/


All Articles