Пример #1
0
        /// <summary>
        /// Insert a new Course and its Department entity by Independent
        /// Association
        /// </summary>
        private static void InsertNewRelatedEntities()
        {
            using (IndependentAssociationEntities context =
                       new IndependentAssociationEntities())
            {
                // Create a new Department entity.
                Department department = new Department()
                {
                    DepartmentID = 6,
                    Name         = "Software Engineering",
                    Budget       = 300000,
                    StartDate    = DateTime.Now
                };

                // Create a new Course entity.
                Course course = new Course()
                {
                    CourseID = 6001,
                    Title    = "Object Oriented",
                    Credits  = 4,
                    StatusID = true,
                    // Set the navigation property.
                    Department = department
                };

                try
                {
                    // Note: Only need to add one entity because the
                    // relationship and related entity will be added
                    // automatically.
                    context.AddToCourses(course);

                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        /// <summary>
        /// Independent Association下插入一条新的Course和Department关联实体
        /// </summary>
        private static void InsertNewRelatedEntities()
        {
            using (IndependentAssociationEntities context =
                       new IndependentAssociationEntities())
            {
                // 创建一个新的Department实体。
                Department department = new Department()
                {
                    DepartmentID = 6,
                    Name         = "Software Engineering",
                    Budget       = 300000,
                    StartDate    = DateTime.Now
                };

                // 创建一个新的Course实体。
                Course course = new Course()
                {
                    CourseID = 6001,
                    Title    = "Object Oriented",
                    Credits  = 4,
                    StatusID = true,
                    // 设置导航属性。
                    Department = department
                };

                try
                {
                    // 注意: 只需要添加一个实体,因为关系和关联实体将自动被添加。
                    context.AddToCourses(course);

                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        /// <summary>
        /// Independent Association下插入一条新的Course和Department关联实体
        /// </summary>
        private static void InsertNewRelatedEntities()
        {
            using (IndependentAssociationEntities context =
                new IndependentAssociationEntities())
            {
                // 创建一个新的Department实体。
                Department department = new Department()
                {
                    DepartmentID = 6,
                    Name = "Software Engineering",
                    Budget = 300000,
                    StartDate = DateTime.Now
                };

                // 创建一个新的Course实体。
                Course course = new Course()
                {
                    CourseID = 6001,
                    Title = "Object Oriented",
                    Credits = 4,
                    StatusID = true,
                    // 设置导航属性。
                    Department = department
                };

                try
                {
                    // 注意: 只需要添加一个实体,因为关系和关联实体将自动被添加。
                    context.AddToCourses(course);

                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }