Attach() 的 LINQ To SQL 异常:无法添加具有已在使用的键的实体

发布于 2024-08-11 14:28:41 字数 778 浏览 3 评论 0原文

考虑这种典型的断开连接场景:

  • 使用 LINQ To SQL 从 SQL Server 加载 Customer 对象
  • ,用户编辑实体,表示层发回修改后的实体。
  • 使用 L2S 的数据层必须将更改发送到 SQL Server

考虑此 LINQ To SQL 查询,其目的是获取 Customer 实体。

Cust custOrig = db.Custs.SingleOrDefault(o => o.ID == c.ID); //get the original
db.Custs.Attach(c, custOrig); //we don't have a TimeStamp=True property
db.SubmitChanges();                

DuplicateKeyException:无法添加具有已在使用的密钥的实体。

alt text

问题

  • 如何避免这种异常?
  • 更新没有/想要/不需要时间戳属性的实体的最佳策略是什么?

次优解决方法

  • 手动将更新客户中的每个属性设置为原始客户。
  • 启动另一个 DataContext

Consider this typical disconnected scenario:

  • load a Customer object from SQL Server using LINQ To SQL
  • user edits the entity, and the presentation tier sends back the entity modified.
  • the data layer, using L2S, must send the changes to SQL Server

Consider this LINQ To SQL query whose intention is to take a Customer entity.

Cust custOrig = db.Custs.SingleOrDefault(o => o.ID == c.ID); //get the original
db.Custs.Attach(c, custOrig); //we don't have a TimeStamp=True property
db.SubmitChanges();                

DuplicateKeyException: Cannot add an entity with a key that is already in use.

alt text

Question

  • How can you avoid this exception?
  • What's the best strategy for updating an entity that does NOT have/want/need a timestamp property?

Sub-Optimal Workarounds

  • manually set each property in the updated customer to the orig customer.
  • spin up another DataContext

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

人疚 2024-08-18 14:28:41

这与您的数据上下文(db)无法多次跟踪同一实体有关。有关所发生情况的更多详细信息,请参阅这篇文章

该帖子底部的一条晦涩评论说要尝试:

public void Update(Customer customer)
{
  NorthwindDataContext context = new NorthwindDataContext();
  context.Attach(customer);
  context.Refresh(RefreshMode.KeepCurrentValues, customer);
  context.SubmitChanges();
}

让我知道它对你来说效果如何,正如该帖子的 OP 所说,这对他来说效果很好......

This has to do with the fact that your datacontext (db) cannot track the same entity more than once. See this post for more details on what's going on.

One of the obscure comments at the bottom of that post says to try:

public void Update(Customer customer)
{
  NorthwindDataContext context = new NorthwindDataContext();
  context.Attach(customer);
  context.Refresh(RefreshMode.KeepCurrentValues, customer);
  context.SubmitChanges();
}

Let me know how it works out for you, as the OP of that post says it worked out for him...

紫南 2024-08-18 14:28:41

您可以这样做,而不是创建新的上下文:

public void Update(Customer customer)
{
    Customer oldCustomer= db.Custs.First(c => c.ID == customer.ID);  //retrieve unedited 
    oldCustomer = customer;  // set the old customer to the new customer.
    db.SubmitChanges();  //sumbit to database
}

Instead of creating a new context you could do this:

public void Update(Customer customer)
{
    Customer oldCustomer= db.Custs.First(c => c.ID == customer.ID);  //retrieve unedited 
    oldCustomer = customer;  // set the old customer to the new customer.
    db.SubmitChanges();  //sumbit to database
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文