当前位置:网站首页>【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
边栏推荐
- AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero
- The round functions in the np, ceil function and floor function
- TF generates uniformly distributed tensor
- POWER SOURCE ETA埃塔电源维修FHG24SX-U概述
- abstract class or interface
- 小黑leetcode之旅:94. 二叉树的中序遍历(补充Morris 中序遍历)
- Tensorflow中placeholder函数的用法
- random.normal() and random.truncated_normal() in TF
- 技术分享 | 接口自动化测试之JSON Schema模式该如何使用?
- 万字总结:分布式系统的38个知识点
猜你喜欢

QGIS编译SIP的问题

Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN

浅谈Numpy中的shape、reshape函数的区别

CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source

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

Word箭头上面怎么打字

Pagoda measurement - building LightPicture open source map bed system

MySQL慢查询的多个原因

角度和弧度的相互换算
Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
随机推荐
AI识万物:从0搭建和部署手语识别系统
Leetcode 93 IP addresses
mysql multi-table left link query
TF生成均匀分布的tensor
SecureCRT sets the timeout period for automatic disconnection
Usage of placeholder function in Tensorflow
什么是IDE(集成开发环境)?
Install Mysql8.0 on windos, and solve the problem of re-login exception ERROR 1045 (28000)
Word第一页不要页眉怎么设置?设置Word首页不要页眉方法教程
编程时请选择正确的输入法,严格区分中英文
Ehrlich screening method: Counting the number of prime numbers
几种绘制时间线图的方法
AI+医疗:使用神经网络进行医学影像识别分析
linux定时执行sql文件[通俗易懂]
Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
mysql配置参数详解[通俗易懂]
TF uses constant to generate data
STC8H development (15): GPIO drive Ci24R1 wireless module
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
Problems with compiling SIP with QGIS