EF多对多更新报错(TableNoTracking引发的bug)
实体映射关系如下,SISTUser和SISTUserRoles存在多对多的关系,生成中间表
public partial class SISTUserMap: EntityTypeConfiguration<SISTUser>
{
public SISTUserMap()
{
this.ToTable("SISTUser");
this.HasKey(u => u.Id);
this.Property(u => u.Id).HasColumnName("UserId"); this.Property(u => u.UserName).HasMaxLength();
this.Property(u => u.Email).HasMaxLength(); this.HasMany(u => u.SISTUserRoles)
.WithMany()
.Map(m => m.ToTable("SISTUser_SISTUserRole_Mapping")); } }
更新操作
public ActionResult SetUserRole(string UserId, string RoleId)
{
try
{
SISTUser user = _userService.GetUserByUserId(UserId);
var allUserRoles = _userService.GetSISTRoles();
foreach (var userRole in allUserRoles)
{
if(userRole.Id==RoleId)
{
if (user.SISTUserRoles.Count(cr => cr.Id == userRole.Id) == )
user.SISTUserRoles.Add(userRole);
} else
{
if (user.SISTUserRoles.Count(cr => cr.Id == userRole.Id) > )
user.SISTUserRoles.Remove(userRole);
}
} _userService.UpdateUser(user); return Json(new BaseJsonResult<object> { success = true, msg = "编辑成功", data = null });
} catch (Exception ex)
{
return Json(new BaseJsonResult<object> { success = false, msg = ex.Message, data = null });
}
}
结果报错:违反了 PRIMARY KEY 约束 'PK_dbo.SISTUserRole'。
原因是下面的查询使用了TableNoTracking,具体底层原理待考察
public virtual IList<SISTUserRole> GetSISTRoles()
{
//使用TableNoTracking会报错
return _sISTUserRoleRepository.Table.ToList();
}
EF多对多更新报错(TableNoTracking引发的bug)的更多相关文章
- EF core2.1+MySQL报错'Void Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBuilder..ctor(Microsoft.EntityFrameworkCore.Storage.IRelationalTypeMapper)
一.使用.net core 2.0 EF mysql 运行一直报错如下: An unhandled exception occurred while processing the request. M ...
- Vue热更新报错(log.error('[WDS] Errors while compiling. Reload prevented.'))
log.error('[WDS] Errors while compiling. Reload prevented.');中的WDS其实是webpack-dev-serverwebpack的意思,用来 ...
- EF执行存储工程报错 String[4]: Size 属性具有无效大小值 0。
EF中执行存储过程报错 String[4]: Size 属性具有无效大小值 0 排查后是如下问题所致,给定的参数没有设定大小(加入红框内的就可以了) private string GetCode(MC ...
- SVN更新报错问题(Please execute the 'Cleanup' command)
SVN更新报错问题(Please execute the 'Cleanup' command) https://segmentfault.com/a/1190000012571289 svn: E20 ...
- druid + mysql + mybatis 批量更新报错
首先 批量更新报错 sql injection violation, multi-statement not allow 然后看了博客:https://blog.csdn.net/qq_3634595 ...
- IIS错误代码500.21 ,Nhibernate更新报错,委托的使用。action传参数
快速阅读 IIS错误代码500.21 ,Nhibernate更新报错,委托的使用.action传参数 IIS错误代码500.21 HTTP 错误 500.21 - Internal Server Er ...
- SVN更新报错
将服务器SVN文件更新到本地是出现下图错误 报错中已经提示可以通过clean up来清理,若直接执行release lock,则不会解决问题. 原因:本地的项目中存在过期的工作副本 解决办法:选择该文 ...
- mybatis批量更新报错badsql
mybatis批量更新时语法写的都对,但是报错,需要在连接上面加上allowMultiQueries=true 示例:jdbc:MySQL://192.168.1.236:3306/test?useU ...
- mybatis批量更新报错
批量更新sql <update id="updateAutoAppraiseInfo" parameterType="Object"> <fo ...
随机推荐
- 网页访问全过程剖析[转].md
本文转载自:http://www.cnblogs.com/wenanry/archive/2010/02/25/1673368.html 原文翻译自:http://igoro.com/archive/ ...
- css常用属性汇总
一.常用css属性 (1) *block(区块) 行高 line-height:数值 | inherit | normal; 字间距 letter-spacing: 数值 | inherit | no ...
- POJ2942:Knights of the Round Table
传送门 点双练习. 很简单的一道模板题,建立反图,求出点双,二分图判定奇环. //POJ 2942 //by Cydiater //2016.11.2 #include <iostream> ...
- centos7安装svn1.8.16
svn下载地址:http://subversion.apache.org/download/ svn要依赖一些包,可以提前装好 yum -y install apr-util apr-util-dev ...
- Android广播接收器BroadcastRceiver
一.使用BroadcastRceiver 1.创建BroadcastRceiver(MyRceiver),重写OnReceiver: public void onReceive(Context con ...
- linux下如何关闭防火墙?如何查看防火墙当前的状态
从配置菜单关闭防火墙是不起作用的,索性在安装的时候就不要装防火墙查看防火墙状态:/etc/init.d/iptables status暂时关闭防火墙:/etc/init.d/iptables stop ...
- Linux2.6.11版本:classic RCU的实现
转载自:http://www.wowotech.net/kernel_synchronization/linux2-6-11-RCU.html 一.前言 无论你愿意或者不愿意,linux kernel ...
- GitHub上值得关注的iOS开源项目
1.AFNetworking地址:https://github.com/AFNetworking/AFNetworking用于网络请求 2.JSONKit地址:https://github.com/j ...
- 如何使用FileZilla上传和下载文件
一.使用FileZilla上传文件 1 打开 FileZilla 按照如下图所示,填写远程 Linux 的 IP ,用户名,密码,还有端口号(默认22) 2 选中左边需要上传的文件,然后拖到右边,等待 ...
- CentOS 6.5 编译 PHP-7 报错:undefined reference to `libiconv_open 无法编译 PHP libiconv
./configure --with-mysql=/backup/mysql --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zli ...