在自动增量字段上设置自定义标识值

发布于 2024-10-31 16:58:30 字数 260 浏览 2 评论 0原文

我的数据库(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 技术交流群。

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

发布评论

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

评论(2

小苏打饼 2024-11-07 16:58:30

我相信除非有某种方法可以在实体框架内关闭“SET Identity_Insert TableName ON”,否则这是不可能的。

基本上在 SQL Server 中,当您在字段上发送身份时,除非您运行以下语句,否则无法手动填充该

SET Identity_Insert TableName ON

字段。运行此语句后,您将能够手动填充身份字段。

部分类为实体框架中的字段创建自己的增量器

我能想到的唯一其他选项是从列中删除 Identity 属性,并使用像这样的

public partial class EntityClassName : global::System.Data.Objects.DataClasses.EntityObject, IEntity
{
    partial void InitializeFields();

    Int64 IEntity.IdentityColumn
    {
        get { return IdentityColumn; }
        set { //some code for an incrementer 
              //and the ability to set manually 
              //if value provide is not null
            }
    }

}

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

SET Identity_Insert TableName ON

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

public partial class EntityClassName : global::System.Data.Objects.DataClasses.EntityObject, IEntity
{
    partial void InitializeFields();

    Int64 IEntity.IdentityColumn
    {
        get { return IdentityColumn; }
        set { //some code for an incrementer 
              //and the ability to set manually 
              //if value provide is not null
            }
    }

}
自由如风 2024-11-07 16:58:30

我不想直截了当地说这是不可能的,但这对于 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.

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