实体框架SET IDENTITY_INSERT [英] Entity Framework SET IDENTITY_INSERT

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

问题描述



当我们有自动递增ID列时,有没有办法强制EF中新实体的ID值? >我们的要求是,在填写或保存之前,我们的创建表单必须始终在空表单上为我们创建的对象显示一个新的唯一ID。这个想法是这个ID可以通过电话读取给某人,然后用户可以在呼叫完成后完成并保存表单。我们可以通过在数据库中插入一个空行来保留一个ID,然后我们有唯一的列和FK;相反,我做了一个下一个ID表,我们增加了锁的安全性,我测试这对对顶部的ID在对象表中也不要小心。这个想法是在我们回写实体时强制使用这个新的ID,但是我看不到如何让EF做到这一点。



可能 - 这只是我错过的一切吗?我不认为该ID甚至使它插入插入,所以我不认为手动调用SET IDENTITY_INSERT围绕SaveChanges将有所帮助。



或者我必须做别的事情我可以看到替代方案:




  • 将我们的ID列更改为不是一个身份,并手动控制它们:这里有一个表ID继承所以这也是棘手的。

  • 将DB ID和用户可见的ID分隔成一个单独的列,并在其中记录我们的唯一ID。

  • 空行保留ID,如上;可能需要一些可空性更改,并修改我们的数据读取代码以忽略这些记录。



谢谢!这是EF4(使用EDMX和生成的类不是POCO),而对于SQL Server 2008而言,这是重要的。

解决方案

为什么不使用Guid作为主键。与自动增量无关,无并发陷阱等。您只需在创建表单时创建Guid。把它交给一个来电者,然后填写表格。当表单被取消时,没有问题。当表单完成后,创建的Guid创建的实体设置实体对象的其他值,将其应用于(a)上下文和SaveChanges()...


Is there a way to force the ID value for a new entity in EF when we have an auto-incrementing ID column, i.e. use SET IDENTITY_INSERT behaviour through EF?

Our requirement is that our create form must always show a new, unique ID for the object we're creating on the empty form before it is filled in or saved. The idea is that this ID can be out read to someone over the phone and then the user can complete and save the form after the call is complete. We could reserve an ID by inserting an empty row into the database there and then, but we have unique columns and FKs; instead I've made a 'next ID' table that we increment with locks for safety, and I test this against the top ID in the object table too to be careful. The idea was to then force the use of this new ID when we write back the entity - but I can't see how to get EF to do it.

Is that possible - is it just something I've missed? I don't think the ID even makes it down to the insert so I don't think manually calling SET IDENTITY_INSERT around the SaveChanges would help.

Or do I have to do something else? I can see alternatives:

  • Change our ID column to not be an identity and take manual control of it all: there's a table ID inheritance here so this is potentially tricky too.
  • Separate DB ID and user-visible ID into a separate column, and record our unique ID there.
  • Empty row to reserve the ID, as above; might need some nullability changes, and amending our data read code to ignore these records.

Thanks! This is EF4 (using an EDMX and generated classes not POCOs), and against SQL Server 2008 in case that matters.

解决方案

Why not use a Guid as primary key. Nothing to do with auto-increment, no concurrency pitfalls etc. You just create the Guid at the moment you create the form. Hand it over to a caller and fill in the form afterwards. When the form is cancelled, no problem. When the form is finished create the entity with the created Guid set the other values of the entity object, apply it to the (a) context and SaveChanges()...

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

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