当前位置:Gxlcms > 数据库问题 > EntityFramework 学习 一 Add Entity Graph using DbContext:

EntityFramework 学习 一 Add Entity Graph using DbContext:

时间:2021-07-01 10:21:17 帮助过:32人阅读

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()); }

 

EntityFramework 学习 一 Add Entity Graph using DbContext:

标签:log   element   var   creat   with   new t   div   eid   ext   

人气教程排行