当前位置:网站首页>【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
边栏推荐
- String hashing (2014 SERC J question)
- 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语言中的文件是什么?
- [Cloud Native] 4.2 DevOps Lectures
- 论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
- STC8H开发(十五): GPIO驱动Ci24R1无线模块
- supervisor 命令操作大全「建议收藏」
- SecureCRT背景配色
- L3-2 至多删三个字符 (30 分)
- Pagoda measurement - building LightPicture open source map bed system
猜你喜欢
别叫我玩,我要考PMP:考PMP选择机构需要了解的那些事儿
CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source
抽象类 or 接口
浅谈Numpy中的shape、reshape函数的区别
The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
Multiple reasons for MySQL slow query
RHEL7系统修复rm -rf /boot /etc/fstab
kvm虚拟机出现启动不了,NOT available,PV大于分区
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
Simple questions peek into mathematics
随机推荐
“稚晖君”为2022昇腾AI创新大赛打call&nbsp;期待广大开发者加入
Ehrlich screening method: Counting the number of prime numbers
MySQL慢查询的多个原因
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
SecureCRT强制卸载
leetcode:数组中的第K个最大元素
Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
TF中random.normal()与random.truncated_normal()
unit test
SQLi-LABS Page-2 (Adv Injections)
QGIS编译SIP的问题
Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum
Technology Sharing | How to use the JSON Schema mode of interface automation testing?
json事例
编译原理之文法
POWER SOURCE ETA埃塔电源维修FHG24SX-U概述
Word文档怎么输入无穷大符号∞
如何让您的公司内容满足 GDPR 合规性
SecureCRT sets the timeout period for automatic disconnection
小黑leetcode之旅:94. 二叉树的中序遍历(补充Morris 中序遍历)