zoukankan      html  css  js  c++  java
  • Entity Framework Tutorial Basics(26):Add Entity Graph

    Add Entity Graph using DbContext:

    Adding entity graph with all new entities is a simple task. We can use DbSet.Add() method to attach a whole entity graph to the context and set all the entity's states to Added as we have seen in theprevious section.

    The following example adds a student entity graph in disconnected mode, using the DbSet.Add() method which marks the added state to all the entities:

    //Create student in disconnected mode
    Student newStudent = new Student() { StudentName = "New Single Student" };
                
    //Assign new standard to student entity
    newStudent.Standard = new Standard() { StandardName = "New Standard" };
                
    //add new course with new teacher into student.courses
    newStudent.Courses.Add(new Course() { CourseName = "New Course for single student", Teacher = new Teacher() { TeacherName = "New Teacher" } });
    
    using (var context = new SchoolDBEntities())
    {
        context.Students.Add(newStudent);
        context.SaveChanges();
    
        Console.WriteLine("New Student Entity has been added with new StudentId= " + newStudent.StudentID.ToString());
        Console.WriteLine("New Standard Entity has been added with new StandardId= " + newStudent.StandardId.ToString());
        Console.WriteLine("New Course Entity has been added with new CourseId= " + newStudent.Courses.ElementAt(0).CourseId.ToString());
        Console.WriteLine("New Teacher Entity has been added with new TeacherId= " + newStudent.Courses.ElementAt(0).TeacherId.ToString());
    }
    Output:

    New Student Entity has been added with new StudentId= 14 
    New Standard Entity has been added with new StandardId= 6
    New Course Entity has been added with new CourseId= 7
    New Teacher Entity has been added with new TeacherId= 9

    This executes the following DDL statements to the database:

    exec sp_executesql N'INSERT [dbo].[Standard]([StandardName], [Description])
    VALUES (@0, NULL)
    SELECT [StandardId]
    FROM [dbo].[Standard]
    WHERE @@ROWCOUNT > 0 AND [StandardId] = scope_identity()',N'@0 varchar(50)',@0='New Standard'
    go
    
    exec sp_executesql N'INSERT [dbo].[Student]([StudentName], [StandardId])
    VALUES (@0, @1)
    SELECT [StudentID]
    FROM [dbo].[Student]
    WHERE @@ROWCOUNT > 0 AND [StudentID] = scope_identity()',N'@0 varchar(50),@1 int',@0='New Single Student',@1=61
    go
    
    exec sp_executesql N'INSERT [dbo].[Teacher]([TeacherName], [StandardId])
    VALUES (@0, NULL)
    SELECT [TeacherId]
    FROM [dbo].[Teacher]
    WHERE @@ROWCOUNT > 0 AND [TeacherId] = scope_identity()',N'@0 varchar(50)',@0='New Teacher'
    go
    
    exec sp_executesql N'INSERT [dbo].[Course]([CourseName], [Location], [TeacherId])
    VALUES (@0, NULL, @1)
    SELECT [CourseId]
    FROM [dbo].[Course]
    WHERE @@ROWCOUNT > 0 AND [CourseId] = scope_identity()',N'@0 varchar(50),@1 int',@0='New Course for single student',@1=93
    go
    
    exec sp_executesql N'INSERT [dbo].[StudentCourse]([StudentId], [CourseId])
    VALUES (@0, @1)
    ',N'@0 int,@1 int',@0=43,@1=72
    go

    Thus, there is no difference when you add a single entity or entity graph in disconnected or connected scenario. Make sure that all the entities are new.

    Learn how to update entity graph in disconnected scenario in the next section.

  • 相关阅读:
    【TIDB】2、TIDB进阶
    【TIDB】1、TiDb简介
    【Tair】淘宝分布式NOSQL框架:Tair
    【ElasticSearch】查询优化
    【高并发解决方案】9、大流量解决方案
    【高并发解决方案】8、Nginx/LVS/HAProxy负载均衡软件的优缺点详解
    【JVM】jdk1.8-jetty-swap被占满问题排查
    【JVM】记录一次线上SWAP偏高告警的故障分析过程
    【JVM】内存和SWAP问题
    【MySQL】mysql索引结构及其原理
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5649139.html
Copyright © 2011-2022 走看看