数据库未使用 Attach 方法更新

Database does not get updated with Attach method(数据库未使用 Attach 方法更新)

本文介绍了数据库未使用 Attach 方法更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 LINQ to SQL.我已经成功地实现了插入方法并且数据被插入到数据库中.当我尝试更新现有数据时,即使没有例外,它也不会反映在数据库中.你能说明一下可能出了什么问题吗?

I am trying to learn LINQ to SQL. I have successfully implemented insert method and data is getting inserted into database. When I try to update the existing data, it does not get reflected in the database even though there is no exception. Can you please put some light on what could have gone wrong?

注意:帐号是主键

编辑

以下代码行使其工作.但是,您能解释一下为什么我们需要更新吗?

Following lines of code makes it working. But, can you explain why we need a refresh?

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

注意:

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

刷新是通过 KeepCurrentValues 选项完成的.我的理解是它将保留我在实体对象中更新的值.我正在提供(仅)需要在数据库中更新的所需信息.是否需要刷新以获取剩余的列值?

The refresh was done with KeepCurrentValues option. What I understand is it will retain the values which I updated in the entity object. I am supplying (only) the required information which need to be updated in the database. Is this refresh required to get the remaining column values?

客户

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:更新时不刷新UpdateCheck = 从不"

Linq To SQL 附加/刷新实体对象

什么是LINQ-to-SQL表;.附加做什么?

为什么我应该在我的 LINQ To SQL 存储库保存方法中使用 GetOriginalEntityState()?

如何才能我拒绝 Linq to SQL 的 DataContext 中的所有更改?

推荐答案

这是按照问题中的答案解决的LINQ to SQL:当UpdateCheck = Never"时更新而不刷新.

This is resolved by following the answer in the question LINQ to SQL: Updating without Refresh when "UpdateCheck = Never".

这里不使用 Refresh,更新语句前没有 select 语句.

This does not use Refresh and there is no select statement before update statement.

这篇关于数据库未使用 Attach 方法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:数据库未使用 Attach 方法更新

基础教程推荐