如何在Entity Framework 4中获取SQL 2008的下一个主键

时间:2021-05-17 02:07:54

I am using an Entity Framework 4 connection to connect to a SQL 2008 server to do database handling for my application.

我正在使用Entity Framework 4连接来连接到SQL 2008服务器,以便为我的应用程序执行数据库处理。

I am using C# in Visual Studio 2010 and my question is regarding Primary Key fields that are incremented by the SQL itself, is it possible at all for me to be able to find the ID that will be used next by the database.

我在Visual Studio 2010中使用C#,我的问题是关于SQL本身增加的主键字段,我是否有可能找到数据库接下来要使用的ID。

The reason why I cannot just find the last item and +1 is because if my table contains items 1,2,3,4 and 5, removing item 5 then adding another will make the next item become item 6, rather than 5 again (As I use the Identity Specification in SQL, but this must be used).

我不能只找到最后一项和+1的原因是因为如果我的表包含项目1,2,3,4和5,删除项目5然后添加另一项将使下一项目成为项目6,而不是5(因为我在SQL中使用身份规范,但必须使用它。

I cannot find any method such as Item.ID.GetNextIdentity() or something like that and have looked through as many similar questions like this but to no avail.

我找不到任何方法,如Item.ID.GetNextIdentity()或类似的东西,并查看了这样多的类似问题,但无济于事。

Any help would be appreciated

任何帮助,将不胜感激

3 个解决方案

#1


9  

There is no reliable way to use auto incremented ID and show it in a form before you do the save. That is wrong architecture. If you want to have ID shown before saving the form you must either:

在执行保存之前,没有可靠的方法来使用自动递增的ID并在表单中显示它。这是错误的架构。如果您想在保存表单之前显示ID,您必须:

  • Not use auto incremented column as ID and handle uniqueness yourselves
  • 不使用自动递增列作为ID并自行处理唯一性

  • Save the form immediately when user starts creating it in some initial empty state and the final form confirmation will do only update
  • 当用户在某个初始空状态下开始创建表单时立即保存表单,最终表单确认只会更新

Why you cannot ask for next ID? Because if you do it in any way nobody says that received ID will be really used for your form. If another process / thread / application inserts form between your ID retrieval and your form persistence, the Id you shown will be assigned to that inserted form.

为什么你不能要求下一个ID?因为如果你以任何方式这样做,没有人说收到的ID将真正用于你的表格。如果另一个进程/线程/应用程序在您的ID检索和表单持久性之间插入表单,则您显示的ID将分配给该插入的表单。

Also if you are using auto incremented primary keys in the database you cannot assign the key value in your application - the value will not be used and database can override it with its own.

此外,如果您在数据库中使用自动递增的主键,则无法在应用程序中分配键值 - 不会使用该值,并且数据库可以使用自己的值覆盖它。

#2


0  

Quite agree with Ladislav Mrnka, though, this kind of functionality could be needed. One bypass suggestion would be :

但是,与Ladislav Mrnka完全一致,可能需要这种功能。一个旁路建议是:

        // Insert existing object in data base via EF, but PK is missing ...
        using (var Es = new MyEntities())
        {
            try
            {
                // Allows you to get next pk
                var e = new  Model.TableXXX();
                // Set pk to existing entity which has no pk
                existingEntity.PkField = e.PkField;
                // Save existing object and let garbage collector take care
                // of newly created entity
                Es.TableXXX.AddObject(existingEntity);
                Es.SaveChanges();
            }
            catch (Exception ex)
            {
                // implement exception
            }
        }

#3


-1  

DBCC CHECKIDENT ( table_name, NORESEED )

DBCC CHECKIDENT(table_name,NORESEED)

Returns the current identity value and the current maximum value of the identity column.

返回标识列的当前标识值和当前最大值。

#1


9  

There is no reliable way to use auto incremented ID and show it in a form before you do the save. That is wrong architecture. If you want to have ID shown before saving the form you must either:

在执行保存之前,没有可靠的方法来使用自动递增的ID并在表单中显示它。这是错误的架构。如果您想在保存表单之前显示ID,您必须:

  • Not use auto incremented column as ID and handle uniqueness yourselves
  • 不使用自动递增列作为ID并自行处理唯一性

  • Save the form immediately when user starts creating it in some initial empty state and the final form confirmation will do only update
  • 当用户在某个初始空状态下开始创建表单时立即保存表单,最终表单确认只会更新

Why you cannot ask for next ID? Because if you do it in any way nobody says that received ID will be really used for your form. If another process / thread / application inserts form between your ID retrieval and your form persistence, the Id you shown will be assigned to that inserted form.

为什么你不能要求下一个ID?因为如果你以任何方式这样做,没有人说收到的ID将真正用于你的表格。如果另一个进程/线程/应用程序在您的ID检索和表单持久性之间插入表单,则您显示的ID将分配给该插入的表单。

Also if you are using auto incremented primary keys in the database you cannot assign the key value in your application - the value will not be used and database can override it with its own.

此外,如果您在数据库中使用自动递增的主键,则无法在应用程序中分配键值 - 不会使用该值,并且数据库可以使用自己的值覆盖它。

#2


0  

Quite agree with Ladislav Mrnka, though, this kind of functionality could be needed. One bypass suggestion would be :

但是,与Ladislav Mrnka完全一致,可能需要这种功能。一个旁路建议是:

        // Insert existing object in data base via EF, but PK is missing ...
        using (var Es = new MyEntities())
        {
            try
            {
                // Allows you to get next pk
                var e = new  Model.TableXXX();
                // Set pk to existing entity which has no pk
                existingEntity.PkField = e.PkField;
                // Save existing object and let garbage collector take care
                // of newly created entity
                Es.TableXXX.AddObject(existingEntity);
                Es.SaveChanges();
            }
            catch (Exception ex)
            {
                // implement exception
            }
        }

#3


-1  

DBCC CHECKIDENT ( table_name, NORESEED )

DBCC CHECKIDENT(table_name,NORESEED)

Returns the current identity value and the current maximum value of the identity column.

返回标识列的当前标识值和当前最大值。