数据库没有得到与Attach方法更新 [英] Database does not get updated with Attach method
问题描述
我努力学习的LINQ to SQL。我已经成功地实现插入方法和数据是越来越插入到数据库中。当我尝试更新现有的数据,它不会反映到数据库中,即使有也不例外。能否请您把一些光什么能出了错?
注:账户号码是主键
修改
继code线使得它的工作。但是,你能解释为什么我们需要一个刷新
accountRepository.UpdateChangesByAttach(ACC1);
accountRepository.RefreshEntity(ACC1);
accountRepository.SubmitChanges();
请注意:
通过在数据库中使用的数据DataContext.Refresh方法刷新对象状态。
刷新与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,实体);
}
}
阅读
-
<一个href="http://stackoverflow.com/questions/11189688/linq-to-sql-updating-without-refresh-when-updatecheck-never">LINQ到SQL:更新无刷新的时候UpdateCheck的=从不
- ?
-
<一个href="http://stackoverflow.com/questions/3472408/why-should-i-use-getoriginalentitystate-in-my-linq-to-sql-repository-save-meth">Why我应该在LINQ使用GetOriginalEntityState()为SQL资料库的保存方法是什么?
-
<一个href="http://stackoverflow.com/questions/259219/how-can-i-reject-all-changes-in-a-linq-to-sqls-datacontext">How我可以拒绝在LINQ的所有更改到SQL的DataContext的?
这是由以下的问题<一个答案解析href="http://stackoverflow.com/questions/11189688/linq-to-sql-updating-without-refresh-when-updatecheck-never">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"
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.
这篇关于数据库没有得到与Attach方法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!