Exemplo n.º 1
0
        /// <summary>
        /// Insert a new Course and set it belong to an existing Department
        /// by Foreign Key Association
        /// </summary>
        private static void InsertByExistingEntities()
        {
            using (FKAssociationEntities context =
                       new FKAssociationEntities())
            {
                // Create a new Course entity.
                Course course = new Course()
                {
                    CourseID = 5002,
                    Title    = "Data Structure",
                    Credits  = 3,
                    StatusID = true,
                    // Set the foreign key property to an existing
                    // Department's DepartmentID.
                    DepartmentID = 5
                };

                try
                {
                    // Add the Course entity into the ObjectContext.
                    context.AddToCourses(course);

                    // Note: The navigation property of the Course
                    // won't map to the Department entity until
                    // after SaveChanges is called.
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 利用Foreign Key Association来插入一条新的Course并将其设置成附属于一个已存在的Department。
        /// </summary>
        private static void InsertByExistingEntities()
        {
            using (FKAssociationEntities context =
                       new FKAssociationEntities())
            {
                // 创建一个新的Course实体。
                Course course = new Course()
                {
                    CourseID = 5002,
                    Title    = "Data Structure",
                    Credits  = 3,
                    StatusID = true,
                    // 设置外键属性为一个已存在的Department实体的DepartmentID。
                    DepartmentID = 5
                };

                try
                {
                    // 将Course加入ObjectContext。
                    context.AddToCourses(course);

                    // 注意: Course实体的导航属性在SaveChanges方法未被调用前不会映射到Department实体。
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Exemplo n.º 3
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);
                }
            }
        }
Exemplo n.º 4
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);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 利用Foreign Key Association来插入一条新的Course并将其设置成附属于一个已存在的Department。
        /// </summary>
        private static void InsertByExistingEntities()
        {
            using (FKAssociationEntities context =
                new FKAssociationEntities())
            {
                // 创建一个新的Course实体。
                Course course = new Course()
                {
                    CourseID = 5002,
                    Title = "Data Structure",
                    Credits = 3,
                    StatusID = true,
                    // 设置外键属性为一个已存在的Department实体的DepartmentID。
                    DepartmentID = 5
                };

                try
                {
                    // 将Course加入ObjectContext。
                    context.AddToCourses(course);

                    // 注意: Course实体的导航属性在SaveChanges方法未被调用前不会映射到Department实体。
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Exemplo n.º 6
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);
                }
            }
        }