zoukankan      html  css  js  c++  java
  • Entity Framework Core系列之DbContext(添加)

    上一篇我们介绍了Entity Framework Core系列之DbContext,对DbContext有了概念上的了解,这篇将介绍DbContext添加数据

    通过DbContext添加实体的主要方法:

    • Add<TEntity>(TEntity entity)
    • Add(object entity)
    • AddRange(IEnumerable<object> entities)
    • AddRange(params object[] entities)

    这些方法是EF Core 中DbContext的新方法,在以前的EF DbContext是没有的。

    通常,您将使用Add的通用版本,可省略类型参数,因为编译器将从传递给方法的参数中推断类型。以下两个例子完全相同:

    当使用任意一版Add方法时,上下文context将跟踪传递给该方法的实体,并将EntityState值应用到该方法中。除此之外,上下文Context还对图中尚未被上下文跟踪的所有其他对象应用添加的EntityState值。在下一个例子中,增加的State值也被应用到Books中:

     var author3 = new Author
                    {
                        FirstName = "yixuan",
                        LastName = "han",
                        Books = new List<Book>{
                            new Book { Title = "Hamlet"},
                            new Book { Title = "Othello" },
                            new Book { Title = "MacBeth" }
                        }
                    };
                    context.Add(author3);
                    context.SaveChanges();

    Books是通过Author的Books属性被引用而增加的。在下一个例子中,不会添加Books:

     var author4 = new Author { FirstName = "yixuan", LastName = "han" };
                    var hamlet = new Book { Title = "Hamlet", Author = author4 };
                    var othello = new Book { Title = "Othello", Author = author4 };
                    var macbeth = new Book { Title = "MacBeth", Author = author4 };
                    context.Add(author4);
                    context.SaveChanges();

    尽快Book中已经实例化Author的属性,但是Author并不知道他们的关系,所以其Books属性还是null,不会被添加到上下文中。

    添加多条记录

    AddRange方法用于在一次方法调用中向数据库添加多个对象。下一个示例中的代码与前面的示例非常相似,但AddRange方法用于将所有的Book和Author一次性保存到数据库中:

     var author5 = new Author { FirstName = "yixuan", LastName = "han" };
                    var books = new List<Book> {
                        new Book { Title = "It", Author = author5 },
                        new Book { Title = "Carrie", Author = author5 },
                        new Book { Title = "Misery", Author = author5 }
                    };
                    context.AddRange(books);
                    context.SaveChanges();

    AddRange方法的这个版本采用了IEnumerable < object>。EF Core非常聪明,可以识别添加到上下文中的对象的类型,并形成适当的SQL。Author和所有的书都有关联,所以也是会被添加到上下文的。

    AddRange方法的另一个版本采用了params数组,并提供了一次性向数据库添加许多不相关对象的功能:

    var author6 = new Author { FirstName = "yixuan", LastName = "han" };
                    var book = new Book { Title = "Adventures of Huckleberry Finn" };
                    context.AddRange(author6, book);
                    context.SaveChanges();

    当在DbContext上调用SaveChanges方法时,所有具有添加状态值EntityState的实体都将被插入到数据库中。

  • 相关阅读:
    【2020省选模拟】01.18比赛总结
    【2020省选模拟】01.17比赛总结
    利用dockerfile 安装一个tomcat7
    docker的基本安装和命令详解
    jumpserver1.4.1 安装过程
    Redis info参数总结
    ansible-service
    ansible-yum
    centos源码安装mariadb和Galera 多主集群
    ansible常用模块
  • 原文地址:https://www.cnblogs.com/yixuanhan/p/9267801.html
Copyright © 2011-2022 走看看