MVC身份2.2.1 - 主键(GUID)作为唯一标识符,而不是为nvarchar(128) [英] MVC Identity 2.2.1 - Primary Key (Guid) as UniqueIdentifier instead of nVarChar(128)

查看:652
本文介绍了MVC身份2.2.1 - 主键(GUID)作为唯一标识符,而不是为nvarchar(128)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前项目:


  • ASP.NET 4.5.2

  • MVC 5

  • 身份2.2.1

一个我遇到的问题是,我利用GUID的整个项目的其余部分正确,与数据库字段类型是唯一标识符。不幸的是,身份不被同样的游戏规则,并为一些高深莫测的原因,他们创建的表中的字段作为一个字符串 - 为nvarchar(128)

One of the problems I am running into is that I am making use of GUIDs properly throughout the rest of the project, with the DB field types being UniqueIdentifier. Unfortunately, Identity does not play by the same rules, and for some inscrutable reason they created the table fields as a string -- nVarChar(128).

我已经找到了几命中在那里,展示如何正确地修改身份使用唯一标识符,并投了GUID来正确的格式,但所有这些资源目前已经过时 - 大多数是<一个href=\"http://stackoverflow.com/questions/19912049/need-to-change-asp-net-identity-rtm-user-id-from-nvarchar128-to-uniqueidenti\">$p$p-v2而最近的一次是在<一个href=\"http://stackoverflow.com/questions/22504951/aspnet-identity-using-guid-as-key\">2.0.0-alpha,它提供的code不匹配或身份的当前版本。

I have found a few hits out there that show how to properly modify Identity to use UniqueIdentifier and cast the Guids to the proper format, however all these resources are currently obsolete - most are for pre-v2 and the most recent one was for 2.0.0-alpha, and the code it provided doesn’t match up or work on the current version of Identity.

我已经做了我最好的谷歌是什么在那里,但它要么不存在,或我的谷歌福今天较弱。我在寻找如何将数据库字段更改为唯一标识符(如适当的GUID),​​同时还具有其他一切工作的指针/正常施放。

I have done my best to Google what is out there, but either it doesn’t exist or my Google-fu is weak today. I am looking for any pointers on how to change the DB field to a UniqueIdentifier (as proper for a Guid) while still have everything else work/cast properly.

编辑:看来,一个人或系统已连接的这篇文章作为一个可能的重复到另一篇文章。问题是,我已经链接到其它制品作为是身份的1.x版的使用,并且因此,不能直接适用。即使是2.0.0-αcode,我联系不工作。即使在联的制品的底部的附加材料没有官能2.2.1下

It seems that a person or system has linked this article as a possible duplicate to another article. Problem is, I already linked to that other article as being a v1.x usage of Identity, and as such, not directly applicable. Even the 2.0.0-alpha code that I linked to does not work. Even the additional material at the bottom of the linked article is not functional under 2.2.1.

推荐答案

也许<一个href=\"http://www.developerhandbook.com/entity-framework/entity-framework-use-a-guid-as-the-primary-key/\"相对=nofollow>这个将帮助:

实体框架 - 使用GUID作为主键

Entity Framework – Use a Guid as the primary key

更新
所以,如果你会使用GUID是这样的:

UPDATE So if you will use GUID like this:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }

EF将生成的唯一标识符的在你的数据库表,但它是非常重要的列值设置为(NEWSEQUENTIALID())的做出的 StoreGeneratedPattern 到身份。
所以这个机器会自动生成下一个唯一的ID

EF will generate uniqueidentifier in your DB table, but it's important that column value to be set to (newsequentialid()) and make StoreGeneratedPattern to Identity. So this will automaticaly generate next unique ID

using (ApplicationDbContext context = new ApplicationDbContext())
{
    var item = new Item
                 {
                     Name = "Random",
                     Description= "Person";
                 };

    context.Items.Add(item);
    context.SaveChanges();
    Console.WriteLine(item.Id);
}

首页,这将有帮助。

这篇关于MVC身份2.2.1 - 主键(GUID)作为唯一标识符,而不是为nvarchar(128)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆