不带 IDENTITY 列的 LINQ 插入
我正在使用 LINQ,但我的数据库表没有 IDENTITY 列(尽管它们使用代理主键 ID 列)
这可以工作吗?
为了获取表的标识值,有一个名为 GetIDValueForOrangeTable() 的存储过程,它查看 SystemValues 表并递增其中的 ID。 有什么方法可以让 LINQ 在插入时从该 SystemValues 表获取 ID 值,而不是内置的 IDENTITY 吗?
顺便说一句,我认为这不是一个好主意,尤其是对于 Web 应用程序而言。 我想由于这个 SystemValues 查找,将会出现很多并发冲突。 我的担心有道理吗?
干杯 邓肯
I'm using LINQ, but my database tables do not have an IDENTITY column (although they are using a surrogate Primary Key ID column)
Can this work?
To get the identity values for a table, there is a stored procedure called GetIDValueForOrangeTable(), which looks at a SystemValues table and increments the ID therein.
Is there any way I can get LINQ to get the ID value from this SystemValues table on an insert, rather than the built in IDENTITY?
As an aside, I don't think this is a very good idea, especially not for a web application. I imagine there will be a lot of concurrency conflicts because of this SystemValues lookup. Am I justified in my concern?
Cheers
Duncan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的担心是非常有道理的。 如果两个用户尝试同时插入,则两个用户可能会获得相同的编号,除非您按照 marc_s 的描述进行操作并将其放入事务中。 但是,如果事务没有环绕整个插入以及包含 id 值的表,那么如果外部插入失败(它获得了一个值,但由于某些其他原因没有插入一个值),您可能仍然会有间隙。记录)。 由于大多数人这样做是为了避免差距(这在大多数情况下是不必要的要求),这使得生活变得更加复杂,并且仍然可能无法实现结果。 使用身份字段几乎总是更好的选择。
You are very justified in your concern. If two users try to insert at the sametime, both might be given the same number unless you do as described by marc_s and put the thing in a transaction. However, if the transaction doesn't wrap around your whole insert as well as the table that contains the id values, you may still have gaps if the outer insert fails (It got a value but then for some other reason didn't insert a record). Since most people do this to avoid gaps (something that is in most cases an unnecessary requirement) it makes life more complicated and still may not achieve the result. Using an identity field is almost always a better choice.
当然,您也可以使用 LINQ 实现这一点,而且也安全:
这应该可以工作 - 不确定它是否比让数据库处理工作更快、更有效 - 但它应该工作 - 并且安全。
Marc
更新:
这样的东西(适应您的需求)将安全地工作:
至于性能 - 只要您有合理数量(可能少于 50 个)的并发用户,并且只要这个
SystemTables
表没有用于其他用途,那么它应该执行正常。Sure you can make this work with LINQ, and safely, too:
That should work - not sure if it's any faster and any more efficient than letting the database handle the work - but it should work - and safely.
Marc
UPDATE:
Something like this (adapt to your needs) will work safely:
As for performance - as long as you have a reasonable number (less than 50 maybe) of concurrent users, and as long as this
SystemTables
tables isn't used for much else, then it should perform OK.