实体框架的Guid [英] Entity Framework Guid

查看:128
本文介绍了实体框架的Guid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图安装Entity Framework和SQL Server 2008中我使用的GUID对我的表的键。有没有一种方法来设置它使键由数据库自动生成?我试图ROWGUID设置为true,并设置列的默认值是(NEWID())。无论哪种方式映射类仍然需要我给它一个GUID在C#的一面。任何想法?

I'm trying to setup the Entity Framework with SQL Server 2008. I'm using Guids for the keys on my tables. Is there a way to set it up so the keys are automatically generated by the database? I tried setting "RowGuid" to true and also set the column's default value to be "(newid())". Either way the mapped class still needs me to give it a Guid on the C# side. Any ideas?

推荐答案

尚未

17.4.Can我使用服务器生成的GUID为我的实体键?

不幸的是,EF的V1不支持此操作。虽然可以使用SQL Server有型唯一标识符一栏,并设置它的默认值是NEWID(),与SQL Server 2000有提取价值,当时生成的服务器没有什么好办法,你做插入。这可以在较新版本的SQL Server来完成,不过,这样的意图是,我们应该找到一个干净的方式特殊情况下,在EF的未来版本中这种支持,以便它可以在这些数据库,允许它,而不是支持其他。就目前来说,解决办法是生成客户端(最好在你的对象的构造函数)的GUID,而不是在服务器上。

Unfortunately, in v1 of the EF this is not supported. While it is possible with SQL Server to have a column of type "uniqueidentifier" and to set it’s default value to be "newid()", with SQL Server 2000 there’s no good way to extract the value that the server generated at the time you do the insert. This can be done on more recent versions of SQL Server, though, so the intent is that we should find a clean way to special case this support in future versions of the EF so it can be supported on those databases that allow it and not on others. For now, the work around is to generate the guid on the client (ideally in the constructor of your object) rather than on the server.

这篇关于实体框架的Guid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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