Esempio n. 1
0
        /// <summary>
        /// Insert a new Course and its Department entity by Foreign Key
        /// Association
        /// </summary>
        private static void InsertNewRelatedEntities()
        {
            using (FKAssociationEntities context =
                       new FKAssociationEntities())
            {
                // Create a new Department entity.
                Department department = new Department()
                {
                    DepartmentID = 5,
                    Name         = "Computer Science",
                    Budget       = 400000,
                    StartDate    = DateTime.Now
                };

                // Create a new Course entity.
                Course course = new Course()
                {
                    CourseID = 5001,
                    Title    = "Operation System",
                    Credits  = 4,
                    StatusID = true,
                    // Set its foreign key property.
                    DepartmentID = department.DepartmentID
                };

                try
                {
                    // Add the Department and Course entities into the
                    // ObjectContext.
                    context.AddToDepartments(department);
                    context.AddToCourses(course);

                    // Note: The navigation properties between the
                    // Department and Course entities won't map to each
                    // other until after SaveChanges is called.
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 利用Foreign Key Association来插入一条新的Course和Department关联实体
        /// </summary>
        private static void InsertNewRelatedEntities()
        {
            using (FKAssociationEntities context =
                       new FKAssociationEntities())
            {
                // 创建一个新的Department实体。
                Department department = new Department()
                {
                    DepartmentID = 5,
                    Name         = "Computer Science",
                    Budget       = 400000,
                    StartDate    = DateTime.Now
                };

                // 创建一个新的Course实体。
                Course course = new Course()
                {
                    CourseID = 5001,
                    Title    = "Operation System",
                    Credits  = 4,
                    StatusID = true,
                    // 设置它的外键属性。
                    DepartmentID = department.DepartmentID
                };

                try
                {
                    // 将Department和Course实体添加到ObjectContext。
                    context.AddToDepartments(department);
                    context.AddToCourses(course);

                    // 注意: Department和Course实体之间的导航属性在SaveChanges方法未被调用前不会映射到彼此。
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 利用Foreign Key Association来插入一条新的Course和Department关联实体
        /// </summary>
        private static void InsertNewRelatedEntities()
        {
            using (FKAssociationEntities context =
                new FKAssociationEntities())
            {
                // 创建一个新的Department实体。
                Department department = new Department()
                {
                    DepartmentID = 5,
                    Name = "Computer Science",
                    Budget = 400000,
                    StartDate = DateTime.Now
                };

                // 创建一个新的Course实体。
                Course course = new Course()
                {
                    CourseID = 5001,
                    Title = "Operation System",
                    Credits = 4,
                    StatusID = true,
                    // 设置它的外键属性。
                    DepartmentID = department.DepartmentID
                };

                try
                {
                    // 将Department和Course实体添加到ObjectContext。
                    context.AddToDepartments(department);
                    context.AddToCourses(course);

                    // 注意: Department和Course实体之间的导航属性在SaveChanges方法未被调用前不会映射到彼此。
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }