数据库未使用 Attach 方法更新
- 作者: 温柔男神帅帅哒
- 来源: 51数据库
- 2022-12-13
问题描述
我正在尝试学习 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.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
