LINQ to SQL 是否仅支持单个工作单元,或者我可以保留上下文并执行 Next 操作吗?
我有一个 Windows 应用程序。我正在尝试通过 DataContext 插入一条记录。它在表中具有唯一标识符。即使我在插入后执行触发器。因此,我在触发器末尾进行选择查询以获取自动生成器编号并避免自动同步错误。由于它是一个 Windows 应用程序,我可以长时间保留上下文。当我创建一个新对象(例如订单)并执行相同的先前操作时,在 SubmitChanges 操作时,它显示不能有重复的键。为什么我不能使用相同的上下文来插入第二条记录?或者我是否需要创建一个新的上下文来插入新的记录?(这个工作单元概念是否出现在这里?)。创建新的上下文是个坏主意,因为我需要再次加载所有数据..
有什么想法吗?
一些代码示例来解释我的情况:
CallCenterLogObjCotext = (CallCenterLogObjCotext == null ? (new CallcenterLogContext) : (CallCenterLogObjCotext));
CallDetail newCallDetailsOpenTicket = new CallDetail();
newCallDetailsOpenTicket.CallPurpose = (from callpuposelist in CallCenterLogObjCotext.CallPurposes
where callpuposelist.CallPurposeID == ((CallPurpose)(cbcallpurpose.SelectedItem)).CallPurposeID
select callpuposelist).FirstOrDefault();
很多这样的设置...
CallCenterLogObjCotext.CallDetails.InsertOnSubmit(newCallDetailsOpenTicket);
CallCenterLogObjCotext.SubmitChanges();
正如我上面提到的,这是单击 Windows 窗体上的 Open Ticket 按钮。我更改了该表单上可用文本框中的 fname、lname 和所有值,并单击了同一按钮。 所以它会再次调用相同的方法。我收到以下指定的错误:
System.Data.Linq.DuplicateKeyException:无法添加具有已在使用的密钥的实体。
I have a windows application. I am trying to insert a record through a DataContext. It has Unique identifier in the table. Even I am executing a trigger after insertion. So I am making a select query in the end of the trigger to get the auto generator number and to avoid auto-sync error. As it's a windows application I can keep the Context for longtime. When I create a new object ( for example order) and do the same previous operation, upon SubmitChanges operation, it shows cannot have duplicate key. Why can't I use this same Context to Insert the second record? Or do I need to create a new Context to insert a new Record?(Does this Unit of work Concept comes here?). Creating new Context is bad idea as I need to load all data again..
Any thought?
Some code sample to explain my situation:
CallCenterLogObjCotext = (CallCenterLogObjCotext == null ? (new CallcenterLogContext) : (CallCenterLogObjCotext));
CallDetail newCallDetailsOpenTicket = new CallDetail();
newCallDetailsOpenTicket.CallPurpose = (from callpuposelist in CallCenterLogObjCotext.CallPurposes
where callpuposelist.CallPurposeID == ((CallPurpose)(cbcallpurpose.SelectedItem)).CallPurposeID
select callpuposelist).FirstOrDefault();
Lots of settings like this ...
CallCenterLogObjCotext.CallDetails.InsertOnSubmit(newCallDetailsOpenTicket);
CallCenterLogObjCotext.SubmitChanges();
As I mentioned above, this is a click on Open Ticket button on windows form. I change the values of fname, lname and all in the textboxes available on that form and clicked the same button.
So it will call the same method again. I get the below specified error:
System.Data.Linq.DuplicateKeyException: Cannot add an entity with a key that is already in use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用同一上下文对象插入多行,请参阅 http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx,http://msdn.microsoft.com/en-us/library/bb425822.aspx 以及其他大量在线示例。重复键问题可能是 linq to sql 配置问题或数据库完整性错误,即,如果表上有自然主键,并且尝试多次插入具有相同自然主键的行。
You can insert more than one row with the same context object, see http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx, http://msdn.microsoft.com/en-us/library/bb425822.aspx, and other numerous online examples. The duplicate key issue could be a linq to sql configuration issue, or a database integrity error, i.e. such as if you have a natural primary key on a table and are trying to insert a row with the same natural primary key more than once.