当实体框架中的表为空时,我们能否获得最大的标识列 [英] Can We Get Max Of A Identity Column When Table Empty In Entity Framework c#

查看:55
本文介绍了当实体框架中的表为空时,我们能否获得最大的标识列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个小型销售系统中显示当前交易的用户ID,我需要在我的程序期间获取最大的我的身份ID,现在我想知道当表为空时我可以获取它吗?例如:Id种子是1000 marshal.tbl_Transaction.max(l => l.Id)+1;当程序开始时我的表为空时,它工作正常吗?感谢

i want to show user Id of current Transaction in a small Sale System that i need to get max of My Identity Id During My program now i want to know that i can get it when table is empty ???? for example: Id seed is 1000 marshal.tbl_Transaction.max(l=>l.Id)+1; is it work true when my table is empty at first of program?????????? thanks

推荐答案



这确实是不正确的方法.您不应将"ID最大值"列用作下一个标识"值.

您的方法存在的问题:两个用户同时打开同一页面以添加新值.他们两个都将分配相同的ID值.数据将不一致.

正确的方法:用户打开此表单时,需要在数据库中添加一个条目.您需要使用 IsValidData 字段再添加一列.除非用户单击保存按钮,否则它应该为false.您需要IsValidData字段,因为用户可能会进入该表单并关闭而不保存.在这种情况下,数据库中有一个无效的条目.您需要以特定的时间间隔从数据库中清除此类条目.尽管此过程变得更加复杂,但是如果需要显示Identity值,则需要进行这样的复杂过程.

让我们看看其他CP专家是否提供了更可行的解决方案.

谢谢
-Amit Gajjar
Hi,

This is really incorrect approach. You should not use Max value of ID column for Next Identity value.

Problem with your approach : At the same time two User open the same page to add New value. And both of them will assign with same ID value. Data will be inconsistent.

Correct approach : When user open this form, you need to add one entry in the database. You need to add one more column with IsValidData field. It should be false until user click the save button. You need IsValidData field because user may come to that form and close without saving. In this scenario you have one invalid entry in database. You need to clear such entry from the database with particular time interval. Although this process become more complicated but if you need Identity value to be display, You need to go for such complex process.

Let''s see if other CP Experts come with more feasible solution.

Thanks
-Amit Gajjar


这篇关于当实体框架中的表为空时,我们能否获得最大的标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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