• 分享关于Entity Framework 进行CRUD操作实验的结果


    我们在使用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条结论相同。

  • 相关阅读:
    ogre terrain
    error list
    Basic Turtorial 3( Ogre wiki)
    操作 Wave 文件(3): 接触 mmio 系列函数
    操作 Wave 文件(5): 获取 Wave 文件的格式信息
    操作 Wave 文件(6): 获取 Wave 文件的波形数据
    关于动态数组指针操作的两个例子
    操作 Wave 文件(7): 建立一个空的 Wave 文件(三种方法)
    操作 Wave 文件(8): 使用 TMediaPlayer 录制 wav 文件
    操作 Wave 文件(10): 输入输出设备与格式支持
  • 原文地址:https://www.cnblogs.com/zuowj/p/4864390.html
Copyright © 2020-2023  润新知