当前位置:网站首页>【EF】数据表全部字段更新与部分字段更新
【EF】数据表全部字段更新与部分字段更新
2022-08-09 21:43:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
在更新数据库的过程中,出现了” 更新条目时出错。有关详细信息,请参见内部异常。“的问题。
原因是想要更新表中的部分字段,所以只给需要修改的字段进行赋值,但是用的方法是对全部字段更新,这样就出现了异常。
更新一张表中全部字段的代码:
<span style="font-family:KaiTi_GB2312;font-size:18px;">//实例化读者表并赋值
ta_readerinfo enReader = new ta_readerinfo()
{
ReaderID = ReaderModel.ReaderID,
Name = ReaderModel.Name,
Roles = ReaderModel.Roles,
Sex = ReaderModel.Sex,
Tel = ReaderModel.Tel,
Email = ReaderModel.Email,
Grade = ReaderModel.Grade,
//不需要更新的字段
//Pwd = "123",
//Photo = "呵呵",
//ManagerID = "1",
//IsEnable = "1",
};
//执行更新操作
this.CurrentDal.Update(enReader);
//将对数据库的操作保存
DbSession.SaveChanges();</span>
更新一张表中部分字段的代码:
<span style="font-family:KaiTi_GB2312;font-size:18px;"> //创建映射规则
Mapper.CreateMap<ReaderViewModel, ta_readerinfo>();
//进行转换
ta_readerinfo ReaderList = Mapper.Map<ta_readerinfo>(ReaderModel);
//需要修改的字段
string[] proNames = { "Name", "Roles", "Sex", "Tel", "Email", "Grade" };
//调用更新操作
this.CurrentDal.Update(ReaderList, a => a.ReaderID == ReaderModel.ReaderID, proNames);
//更新SaveChanges
this.DbSession.SaveChanges();</span>
这里用到了Mapper,所以需要添加AutoMapper的引用。
初次接触EF,和直接用SQL语句相比还是不太熟悉,接下来需要多多练习。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/105733.html原文链接:https://javaforall.cn
边栏推荐
- 别叫我玩,我要考PMP:考PMP选择机构需要了解的那些事儿
- Install win virtual machine on VMware
- Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
- Don't tell me to play, I'm taking the PMP exam: what you need to know about choosing an institution for the PMP exam
- 重要的不是成为海贼王,而是像路飞一样去冒险
- C语言中的文件是什么?
- Shanghai Konan SmartRocket series product introduction (3): SmartRocket iVerifier computer interlocking system verification tool
- L3-2 至多删三个字符 (30 分)
- 编程语言中,取余和取模的区别
- 场效应管Mosfet之雷卯Leiditech对应英飞凌Infineon
猜你喜欢
SQL语句及索引的优化
《强化学习周刊》第57期:DL-DRL、FedDRL & Deep VULMAN
Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)
TF generates uniformly distributed tensor
“稚晖君”为2022昇腾AI创新大赛打call&nbsp;期待广大开发者加入
Sudoku | Backtrack-7
筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
Several ways to draw timeline diagrams
简单问题窥见数学
Pagoda measurement - building LightPicture open source map bed system
随机推荐
PHP 二维数组根据某个字段排序
宝塔实测-搭建LightPicture开源图床系统
Word怎么制作一张标准的答题卡?
Bean life cycle
AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero
6个规则去净化你的代码
The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
json case
Sudoku | Backtrack-7
Tensorflow中使用convert_to_tensor去指定数据的类型
筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
Hessian Matrix 海森矩阵
Don't tell me to play, I'm taking the PMP exam: what you need to know about choosing an institution for the PMP exam
单元测试
Wps下划线怎么弄?Wps添加下划线的最全方法
FET Mosfet Leiditech corresponds to Infineon Infineon
L3-2 至多删三个字符 (30 分)
mysql配置参数详解[通俗易懂]
SecureCRT强制卸载
重要的不是成为海贼王,而是像路飞一样去冒险