在自动增量字段上设置自定义标识值
我的数据库(Sql server 2008)中有一个设置了自动数字的 id 列。 我正在使用 EF 和 linq2entities
在某些特定场景中,我希望能够设置自定义 ID 号(显然我完全确定该值不会重复),例如我会用它来“填充”缺失的 ID 号删除引起的。我想在数据库中保留自动增量属性,问题是当我执行 linq 语句时,数据库分配下一个 Id 编号,而不是我喜欢的编号。
也许这有点奇怪,但是可以使用 linq2entities 来做吗?
提前致谢!
I have in my DB (Sql server 2008) a id column with auto numeric set on.
I'm using EF and linq2entities
In some specific scenario I would like to be able to set a custom Id number (obviously I'm totally sure this value is not repeated), for example I would use it to "fill" missing Id numbers caused by deletions. I want to keep the auto increment prop in database, the problem is that when I do the linq sentence, the database assign the next Id number, not the one that I like.
Maybe it's a little weird but is it possible to do using linq2entities ?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信除非有某种方法可以在实体框架内关闭“SET Identity_Insert TableName ON”,否则这是不可能的。
基本上在 SQL Server 中,当您在字段上发送身份时,除非您运行以下语句,否则无法手动填充该
字段。运行此语句后,您将能够手动填充身份字段。
部分类为实体框架中的字段创建自己的增量器
我能想到的唯一其他选项是从列中删除 Identity 属性,并使用像这样的
I believe Its not possible unless there is some way to turn off "SET Identity_Insert TableName ON" within Entity Framework.
Basically in SQL Server when you sent Identity on a field it cannot be populated manually unless you run the following statement
After running this statement you will be able to populate Identity Fields manually.
The only other options I can think of is to remove the Identity attribute from the column and create your own incrementer for the field in the Entity Framework using a partial Class
Something like this
我不想直截了当地说这是不可能的,但这对于 L2E 棒球来说是相当不错的。但这是一个非常简单的 INSERT 触发器。你将通过 INSERTED 获得插入的行(谷歌将解释),然后你用你想要的任何疯狂逻辑更新该行。
我认为你可以在 L2E 上花费几个小时来尝试找出答案,或者用触发器在二十分钟内完成它。
I don't like to say flat-out that it's impossible, but this is pretty inside baseball for L2E. But it's a pretty simple INSERT trigger. You'll get the inserted row via INSERTED (Google will explain), and then you update that row with whatever crazy logic you want.
I think you can bang your head against L2E for hours trying to figure it out, or do it inside of twenty minutes with a trigger.