Database does not get updated with Attach method

2020-02-13 04:58发布

问题:

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?

Note: AccountNumber is primary key

EDIT

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

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

Note:

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

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?

Client

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();


   }

Business Layer

    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();

    }

Repository

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);
    }


}

READING

  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?

回答1:

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

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



回答2:

I made it working as follows. But I am still wondering why we need a Refresh for this to work. Can you please explain it?

Added function in repository

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

Call changed as

    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();

    }