使用Attach方法无法更新数据库

时间:2012-06-22 09:49:43

标签: c# .net sql-server linq linq-to-sql

我正在尝试学习LINQ to SQL。我已经成功实现了insert方法,并且数据被插入到数据库中。当我尝试更新现有数据时,即使没有异常,它也不会反映在数据库中。你能否说一下可能出错的地方?

注意:AccountNumber是主键

修改

以下代码行使其正常工作。但是,你能解释为什么我们需要刷新

    accountRepository.UpdateChangesByAttach(acc1);
    accountRepository.RefreshEntity(acc1);
    accountRepository.SubmitChanges();

注意:

DataContext.Refresh method refreshes object state by using data in the database.

使用KeepCurrentValues选项完成刷新。我的理解是它将保留我在实体对象中更新的值。我提供(仅)需要在数据库中更新的所需信息。此刷新需要才能获得剩余的列值吗?

客户端

using (var context = new   RepositoryLayer.LibraryManagementClassesDataContext(connectionstring)) 
   {
            context.Log = Console.Out;

            RepositoryLayer.Repository<RepositoryLayer.Account> selectedRepository = new RepositoryLayer.Repository<RepositoryLayer.Account>();
            //selectedRepository.Context = context;
            AccountBusiness accountBl = new AccountBusiness(selectedRepository);

            //Transaction 1
            //List<RepositoryLayer.Account> accountList = accountBl.GetAllAccounts();

            //Transaction 2
            //accountBl.InsertAccounts();

            //Transaction3
            accountBl.UpdateAccounts();


   }

业务层

    public void  InsertAccounts()
    {
        RepositoryLayer.Account acc1 = new RepositoryLayer.Account();
        acc1.AccountNumber = 4;
        acc1.AccountType = "Contract";
        acc1.Duration = 6;

        accountRepository.InsertOnSubmit(acc1);
        accountRepository.SubmitChanges();

    }

    public void UpdateAccounts()
    {
        RepositoryLayer.Account acc1 = new RepositoryLayer.Account();
        acc1.AccountNumber = 4;
        acc1.AccountType = "TEST";
        acc1.Duration = 10;

        accountRepository.UpdateChangesByAttach(acc1);
        accountRepository.SubmitChanges();

    }

存储库

public class Repository<T> : IRepository<T> where T : class
{
    public System.Data.Linq.DataContext Context
    {
        get;
        set;
    }

    public virtual System.Data.Linq.ITable GetTable()
    {
        return Context.GetTable<T>();
    }


    public virtual void InsertOnSubmit(T entity)
    {
        GetTable().InsertOnSubmit(entity);
    }

    public virtual void UpdateChangesByAttach(T entity)
    {
        GetTable().Attach(entity);
    }


    public virtual void SubmitChanges()
    {
        Context.SubmitChanges();
    }

    public virtual void RefreshEntity(T entity)
    {
        Context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, entity);
    }


}

阅读

  1. LINQ to SQL: Updating without Refresh when “UpdateCheck = Never”

  2. Linq To SQL Attach/Refresh Entity Object

  3. What does LINQ-to-SQL Table<T>.Attach do?

  4. Why should I use GetOriginalEntityState() in my LINQ To SQL repository save method?

  5. How can I reject all changes in a Linq to SQL's DataContext?

2 个答案:

答案 0 :(得分:1)

通过问题LINQ to SQL: Updating without Refresh when “UpdateCheck = Never”中的答案解决了这个问题。

这不使用Refresh,并且在update语句之前没有select语句。

答案 1 :(得分:0)

我按照以下方式开展工作。但我仍然想知道为什么我们需要刷新才能工作。你能解释一下吗?

在存储库中添加了功能

    public virtual void RefreshEntity(T entity)
    {
        Context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, entity);
    }

呼叫已更改为

    public void UpdateAccounts()
    {
        RepositoryLayer.Account acc1 = new RepositoryLayer.Account();
        acc1.AccountNumber = 4;
        acc1.AccountType = "TEST";
        acc1.Duration = 10;

        accountRepository.UpdateChangesByAttach(acc1);
        accountRepository.RefreshEntity(acc1);
        accountRepository.SubmitChanges();

    }