Database does not get updated with Attach method(数据库未使用 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);
}
}
阅读
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 方法更新
基础教程推荐
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 创建属性设置器委托 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01