当前位置:网站首页>【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
边栏推荐
- Simple questions peek into mathematics
- Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum
- Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
- mysql multi-table left link query
- 万字总结:分布式系统的38个知识点
- 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
- 数独 | 回溯-7
- Technology Sharing | How to use the JSON Schema mode of interface automation testing?
- abstract class or interface
- mysql配置参数详解[通俗易懂]
猜你喜欢
随机推荐
《强化学习周刊》第57期:DL-DRL、FedDRL & Deep VULMAN
How to Make Your Company Content GDPR Compliant
2.1.5 大纲显示问题
random.normal() and random.truncated_normal() in TF
如何让您的公司内容满足 GDPR 合规性
ACM MM 2022 | Cloud2Sketch: 长空云作画,AI笔生花
STC8H开发(十五): GPIO驱动Ci24R1无线模块
Technology Sharing | How to use the JSON Schema mode of interface automation testing?
The round functions in the np, ceil function and floor function
L3-2 至多删三个字符 (30 分)
Word怎么制作双面席卡?使用Word制作双面席卡方法
宝塔实测-搭建LightPicture开源图床系统
Leetcode 93 复原IP地址
Pagoda measurement - building LightPicture open source map bed system
PMP每日一练 | 考试不迷路-8.8(包含敏捷+多选)
APP automation test framework - UiAutomator2 introductory
unit test
The overall construction process of the Tensorflow model
PHP 2D array sorted by a field
RHEL7系统修复rm -rf /boot /etc/fstab