博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分享关于Entity Framework 进行CRUD操作实验的结果
阅读量:6588 次
发布时间:2019-06-24

本文共 4934 字,大约阅读时间需要 16 分钟。

我们在使用Entity Framework框架进行CRUD时,经常会出现各种各样的错误,下面请看我的实验结果。

以下是只用一个上下文对象进行操作:

第一次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.Single(t => t.Id == 2);            post.AuthorId = 1;            blog.SaveChanges();

结果:成功

第二次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.Single(t => t.Id == 2);            post.Author = blog.Authors.Single(t => t.Id == 3);            blog.SaveChanges();

结果:成功

第三次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.Single(t => t.Id == 2);            post.AuthorId = 1;            post.Author = blog.Authors.Single(t => t.Id == 4);            blog.SaveChanges();

结果:失败,报错如下:

Conflicting changes to the role 'Post_Author_Target' of the relationship 'ConsoleApplication1.DDD.Infrastructure.Post_Author' have been detected.

第四次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.Single(t => t.Id == 2);            post.Author = new Author() { Id = 4 };            blog.Entry(post.Author).State = EntityState.Unchanged;            blog.SaveChanges();

结果:成功

第五次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.Single(t => t.Id == 2);            //author = new Author() { Id = 1, Name = "zwj" };//直接实例化或下面查询得出            author = blog.Authors.AsNoTracking().Single(t => t.Id == 3);            post.Author = author;            blog.SaveChanges();

结果:成功,但是Authors表中会新增一笔记录,并将新的ID赋值给Posts表;原因:Author与Post不在同一个上下文中,Post所在的上下文中追踪不到Author的信息,故当成新增。

以下是在两个不同的上下文中进行操作:

第六次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.AsNoTracking().Single(t => t.Id == 2);            //blog.Entry(post).State = EntityState.Detached;上面的AsNoTracking可以由这句实现            BlogDbContext blog2 = new BlogDbContext();            post.AuthorId = 5;            blog2.Entry(post).State = EntityState.Modified;            blog2.SaveChanges();

结果:成功

第七次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.AsNoTracking().Single(t => t.Id == 2);            BlogDbContext blog2 = new BlogDbContext();            post.Author = blog2.Authors.Single(t => t.Id == 5);            blog2.Entry(post).State = EntityState.Modified;            blog2.SaveChanges();

结果:失败,报错如下:

A referential integrity constraint violation occurred: The property value(s) of 'Author.Id' on one end of a relationship do not match the property value(s) of 'Post.AuthorId' on the other end.

第八次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.AsNoTracking().Single(t => t.Id == 2);            BlogDbContext blog2 = new BlogDbContext();            author = blog2.Authors.AsNoTracking().Single(t => t.Id == 1);            post.Author = author;            blog2.Entry(post).State = EntityState.Modified;            blog2.SaveChanges();

结果:失败,报错同第七次

第九次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.AsNoTracking().Single(t => t.Id == 2);            author = blog.Authors.AsNoTracking().Single(t => t.Id == 3);            BlogDbContext blog2 = new BlogDbContext();            post.Author = author;            blog2.Entry(post).State = EntityState.Modified;            blog2.SaveChanges();

结果:失败,报错同第七次

第十次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.AsNoTracking().Single(t => t.Id == 2);            BlogDbContext blog2 = new BlogDbContext();            author = new Author() { Id = 1, Name = "zwj" };            post.Author = author;            blog2.Entry(post).State = EntityState.Modified;            blog2.SaveChanges();

结果:失败,报错同第七次

第十一次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.AsNoTracking().Single(t => t.Id == 2);            BlogDbContext blog2 = new BlogDbContext();            author = blog2.Authors.Single(t => t.Id == 1);            post.Author = author;            post.AuthorId = author.Id;            blog2.Entry(post).State = EntityState.Modified;            blog2.SaveChanges();

结果:成功,但我认为主要是通过赋值AuthorId来完成的,与第六次相同,去掉赋值AuthorId,则与第七次相同的报错

第十二次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.AsNoTracking().Single(t => t.Id == 2);            BlogDbContext blog2 = new BlogDbContext();            author = blog2.Authors.Single(t => t.Id == 3);            blog2.Entry(post).Reference(t => t.Author).CurrentValue = author;            blog2.Entry(post).State = EntityState.Modified;            blog2.SaveChanges();

结果:失败,报错同第七次

第十三次:

BlogDbContext blog = new BlogDbContext();            post = blog.Posts.AsNoTracking().Single(t => t.Id == 2);            BlogDbContext blog2 = new BlogDbContext();            blog2.Posts.Attach(post);            post.Author = blog2.Authors.Single(t => t.Id == 1);            blog2.SaveChanges();

结果:成功

 

最后得出结论:

1.在同一个上下文中,不论是采用直接赋值导航属性或是直接赋值外键属性,除不能同时对导航属性及外键属性赋不相关联的值外,都可以成功;

2.不在同一个上下文中,若想在完成CUD时,则必需先确保要进行操作的实体处于Detached状态,然后再进行相应的更新,涉及导航属性时,只能采用赋值外键属性,不能通过导航属性直接赋值,否则会报错(这个我觉得是个BUG,不知大家有什么好的解决办法没有),若采用先在新的上下文中Attached,再进行更新操作则与第1条结论相同。

转载地址:http://lyhno.baihongyu.com/

你可能感兴趣的文章
前端05.js入门之BOM对象与DOM对象。
查看>>
CISCO路由器NTP服务器配置
查看>>
oracle kill所有plsql developer进程
查看>>
12c rac 实例无法启动之磁盘组空间耗尽
查看>>
keepalived双机热备原理及实例部署LVS+keepalived
查看>>
曲线学习PyQt5方案一
查看>>
Android自定义控件及自定义属性
查看>>
死磕Spring AOP系列2:剖析Bean处理器之BeanNameAutoProxyCreator
查看>>
如何获得查询的执行计划?(一)
查看>>
这些符号你会打吗?
查看>>
云场景实践研究第13期:新浪微博DCP系统
查看>>
Vbs程序批量修改防火墙路由
查看>>
Asp.net报错汇总:回发或回调参数无效
查看>>
linux抓包工具:tcpdump 工具用法
查看>>
二分查找算法
查看>>
【转载】谁动了摩卡的奶酪?
查看>>
爬虫采集-基于webkit核心的客户端Ghost.py [爬虫实例]
查看>>
使用WiX制作具有时间限制的安装包
查看>>
企业私有云之rabbitmq高可用
查看>>
C#的四个基本技巧
查看>>