使用DataContext Attach方法更新实体时出现“更新检查"问题 [英] Update check' issue when updating an entity using DataContext Attach method

查看:70
本文介绍了使用DataContext Attach方法更新实体时出现“更新检查"问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在通用存储库中创建一种更新方法,作为LINQ to SQL数据访问层.

I'm trying to create an update method in a generic repository as a LINQ to SQL data access layer.

我有一个这样的实体:

[Table]
public class Product
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, 
     DbType = "Int NOT NULL IDENTITY")]
    public int Id { get; private set; }
    [Column(UpdateCheck = UpdateCheck.Never)]
    public string Name { get; set; }
    ....
}

我为所有ID以外的字段设置了Update Check = true,如@jeff Atwood在asModified属性设置为true /entity-attachment-issues-in-linq>这篇文章如下:

I set Update Check = true for all the fields exept for the id as @jeff Atwood suggests in this post and I set the asModified propery in the attach method to true which i found in this post as following:

public void Update(T entity)
{
    _db.GetTable<T>().Attach(entity, true); 
    _db.SubmitChanges();
}

但我不断遇到同样的异常:

but I keep getting the same exception:

只有在以下情况下,实体才能以修改后的形式附加而没有原始状态: 它声明版本成员或没有更新检查策略.

An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy.

那是什么问题?

除了将timestamp列创建为版本号之外,是否建议任何其他方法在通用存储库中创建更新方法.

Do you recommend any other approaches to create an update method in a generic repository except creating a timestamp column as a version number.

推荐答案

我们在DAO中使用了以下代码来解决同一问题:
(例如,仅此刻,我身上没有真正的代码)

We used the following code in our DAO to solve the same issue:
(e.g. only, don't have the real code on me at the moment)

public void UpdateUser(tblUser user)
{
   WriteDataContect.Attach
   (
      user,
      ReadOnlyDataContext.tblUsers
                         .Select(o => o.UserId == user.UserId)
   );
   WriteDataContext.SubmitChanges();
}

ReadOnlyDataContext的TrackChanges = false;

ReadOnlyDataContext has TrackChanges = false;

我们无法根据需要找到其他解决方案,而无需编写大量管道代码. 修改数据库以适应LinqToSql对 timestamp 列的需求也不是我们的选择.

We couldn't find another solution based on our needs, without having to write alot of plumbing code. Modifying the database to accommodate LinqToSql's need for a timestamp column wasn't an option for us either.

额外的数据库调用在我们的测试中没有产生任何问题.

The additional DB call didn't create any issues in our testing.

这篇关于使用DataContext Attach方法更新实体时出现“更新检查"问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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