简体   繁体   English

ASP.NET MVC 6 - 为实体框架7中的主键分配NOT NULL

[英]ASP.NET MVC 6 - Assigning NOT NULL to the Primary Key in Entity Framework 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'. 我收到错误消息为Cannot define PRIMARY KEY constraint on nullable column in table 'AspNetRoles'.

Namespace of MigrationBuilder - Microsoft.Data.Entity.Relational.Migrations.Builders MigrationBuilder的命名空间 - Microsoft.Data.Entity.Relational.Migrations.Builders

How to assign NOT NULL to the above code in ASP.NET MVC 6 如何为ASP.NET MVC 6中的上述代码分配NOT NULL

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. 对于能够编写一些代码优先的EF实体并推进的非常临时的解决方法,我注释掉了处理这些密钥的迁移脚本部分。

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. 显然,这会对ASP.NET用户和角色表中的关系产生影响,并且不是一个可行的长期解决方案,我认为它将被修复或者有人会在某个时候找到真正的解决方案。

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? 这可能是EF7早期版本中的错误或缺失功能?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM