数据库没有得到与Attach方法更新数据库、方法、Attach

2023-09-04 00:17:32 作者:找个好姑娘

我努力学习的LINQ to SQL。我已经成功地实现插入方法和数据是越来越插入到数据库中。当我尝试更新现有的数据,它不会反映到数据库中,即使有也不例外。能否请您把一些光什么能出了错?

注:账户号码是主键

修改

继code线使得它的工作。但是,你能解释为什么我们需要一个刷新

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

请注意:

 通过在数据库中使用的数据DataContext.Refresh方法刷新对象状态。
 
数据库怎么不能没有新建表啊,在vs2012中添加数据库,

刷新与KeepCurrentValues​​选项来完成。我的理解是它会保留我的实体对象更新的值。我提供(只),其需要在数据库中被更新所需的信息。是否需要此刷新以获得剩余的列值?

客户端

 使用(VAR上下文=新RepositoryLayer.LibraryManagementClassesDataContext(的ConnectionString))
   {
            context.Log = Console.Out;

            RepositoryLayer.Repository< RepositoryLayer.Account> selectedRepository =新RepositoryLayer.Repository< RepositoryLayer.Account>();
            //selectedRepository.Context =背景;
            AccountBusiness accountBl =新AccountBusiness(selectedRepository);

            //事务1
            //List<RepositoryLayer.Account>为accountList = accountBl.GetAllAccounts();

            //事务2
            //accountBl.InsertAccounts();

            // Transaction3
            accountBl.UpdateAccounts();


   }
 

业务层

 公共无效InsertAccounts()
    {
        RepositoryLayer.Account ACC1 =新RepositoryLayer.Account();
        acc1.AccountNumber = 4;
        acc1.AccountType =合同;
        acc1.Duration = 6;

        accountRepository.InsertOnSubmit(ACC1);
        accountRepository.SubmitChanges();

    }

    公共无效UpdateAccounts()
    {
        RepositoryLayer.Account ACC1 =新RepositoryLayer.Account();
        acc1.AccountNumber = 4;
        acc1.AccountType =TEST;
        acc1.Duration = 10;

        accountRepository.UpdateChangesByAttach(ACC1);
        accountRepository.SubmitChanges();

    }
 

信息库

 公共类资源库&LT; T&GT; :IRepository&LT; T&GT;其中T:类
{
    公共System.Data.Linq.DataContext上下文
    {
        得到;
        组;
    }

    公共虚拟System.Data.Linq.ITable GetTable()
    {
        返回Context.GetTable&LT; T&GT;();
    }


    公共虚拟无效InsertOnSubmit(T实体)
    {
        。GetTable()InsertOnSubmit(实体);
    }

    公共虚拟无效UpdateChangesByAttach(T实体)
    {
        。GetTable()将(实体);
    }


    公共虚拟无效的SubmitChanges()
    {
        Context.SubmitChanges();
    }

    公共虚拟无效RefreshEntity(T实体)
    {
        Context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues​​,实体);
    }


}
 

阅读

LINQ到SQL:更新无刷新的时候UpdateCheck的=从不

的LINQ to SQL连接/刷新实体对象

什么LINQ到SQL表&LT; T&GT ; .Attach做

Why我应该在LINQ使用GetOriginalEntityState()为SQL资料库的保存方法是什么?

How我可以拒绝在LINQ的所有更改到SQL的DataContext的?

解决方案

这是由以下的问题LINQ到SQL:更新无刷新的时候UpdateCheck的=从不

这不使用刷新并没有select语句的更新语句之前。

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

LINQ to SQL: Updating without Refresh when "UpdateCheck = Never"

Linq To SQL Attach/Refresh Entity Object

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

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

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

解决方案

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.