实体框架 - 使用 GUID 作为 EntityKey 的 SaveChanges
我有一个 SQL Server 2008 数据库表,它使用 uniqueidentifier 作为主键。插入时,密钥是使用 newid() 函数在数据库端生成的。
这适用于 ADO.NET。但是当我将此表设置为 Entity Framework 4 模型中的实体时,出现了问题。我能够很好地查询实体,但是当创建新实体并在上下文中调用 SaveChanges() 时,数据库上生成的唯一标识符全为零。
据我所知,EF v1 存在一个问题,此方案不起作用,需要在调用 SaveChanges 之前在客户端上创建 GUID。但是,我在很多地方读到他们计划在 EF 4 中修复此问题。
我的问题是 EF4 中仍然不支持这种情况(数据库端生成唯一标识符)吗?我们是否仍然坚持在客户端生成 GUID?
I have a SQL Server 2008 database table that uses uniqueidentifier as a primary key. On inserts, the key is generated on the database side using the newid() function.
This works fine with ADO.NET. But when I set up this table as an entity in an Entity Framework 4 model, there's a problem. I am able to query the entity just fine, but when creating a new entity and invoking SaveChanges() on the context, the generated uniqueidentifier on the database is all zeros.
I understand there was an issue with EF v1 where this scenario did not work, requiring creating the GUID on the client prior to calling SaveChanges. However, I had read in many places that they were planning to fix this in EF 4.
My question -- is this scenario (DB-side generation of uniqueidentifier) still not supported in EF4? Are we still stuck with generating the GUID on the client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
谢谢各位,终于明白了。在这里写下答案:
https://web.archive.org/web/20100518175148/http://leedumond.com/blog/using-a-guid-as-an-entitykey-in-实体框架-4/
Thanks guys, finally got it figured out. Blogged the answer here:
https://web.archive.org/web/20100518175148/http://leedumond.com/blog/using-a-guid-as-an-entitykey-in-entity-framework-4/
是的,这一点在 EF 4 中发生了变化。您现在可以使用服务器生成的 GUID。 @MusiGenesis,服务器生成的 GUID 有一些优点;它们可以是连续的,例如
Yes, this changed in EF 4. You can now use a server generated GUID. @MusiGenesis, server-generated GUIDs have some advantages; they can be sequential, e.g.