实体框架 - 使用 GUID 作为 EntityKey 的 SaveChanges

发布于 2024-09-01 06:33:43 字数 375 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜点 2024-09-08 06:33:43

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文