zoukankan      html  css  js  c++  java
  • EntityFramework 学习 一 Add Entity Graph using DbContext:

    //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());
    }
  • 相关阅读:
    python入门之函数及其方法
    Python入门知识点2---字符串
    Python列表 元组 字典 以及函数
    Python入门知识
    Autofac使用代码
    优化EF以及登录验证
    CRM框架小知识以及增删查改逻辑代码
    分页SQL
    触发器SQL
    动态生成lambda表达式
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6618625.html
Copyright © 2011-2022 走看看