实体框架指导 [英] Entity Framework Guid

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

问题描述

我正在尝试使用SQL Server 2008设置实体框架。我使用Guids作为表上的键。有没有办法设置,所以键是由数据库自动生成的?我尝试将RowGuid设置为true,并将列的默认值设置为(newid())。无论哪种方式,映射类仍然需要我在C#方面给它一个Guid。任何想法?

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。我可以使用服务器生成的guid作为我的实体密钥吗?

不幸的是,在v1的v1中不支持。虽然SQL Server可以使用uniqueidentifier类型的列,并将其默认值设置为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.

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

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