当前位置:网站首页>【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
边栏推荐
猜你喜欢

微软word怎么转换成pdf文件?微软word转换为pdf格式的方法

MySQL慢查询的多个原因

Install win virtual machine on VMware

如何让您的公司内容满足 GDPR 合规性

Install Mysql8.0 on windos, and solve the problem of re-login exception ERROR 1045 (28000)

在VMware上安装win虚拟机

必看设计干货|易知微设计师是怎么做标准可视化设计服务的?

Word怎么设置图片衬于文字下方?两种方法教你设置Word图片衬于文字下方

AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis

kvm虚拟机出现启动不了,NOT available,PV大于分区
随机推荐
几种绘制时间线图的方法
RHEL7系统修复rm -rf /boot /etc/fstab
SecureCRT background color
Endpoint mode for NetCore routing
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
Optimization of SQL Statements and Indexes
Jensen (琴生) 不等式
LeetCode26:删除有序数组中的重复项
The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
mysql配置参数详解[通俗易懂]
Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
leetcode:数组中的第K个最大元素
Problems with compiling SIP with QGIS
Referenced file contains errors 完美解决方法
什么是IDE(集成开发环境)?
字符串哈希(2014 SERC J题)
TF中random.normal()与random.truncated_normal()
单元测试
编程语言中,取余和取模的区别
The round functions in the np, ceil function and floor function