实体框架6更新表并插入外键相关表

时间:2015-05-24 04:42:30

标签: c# entity-framework

不确定如何为这个标题写标题,如果不准确,请随时编辑。

使用一个例子,我想要完成的是更新表foo中的记录,然后在后续表中创建新记录,其中foo表PK为外键,认为是一对多关系。

如何更新具有外键约束的表并在这些后续表中创建新的相关记录?

目前,我正在将Entity Framework 6与.Add和.Attach实体一起使用到上下文中,并将它们保存到数据库中。

修改

为了进一步澄清我想要实现的目标,下面的对象是一个减少的例子,我试图保存到上下文中。如果我尝试在“Billy Bob”创建之后添加intObj,因为他已经购买了新车,另一项服务,或者他的轮胎已经更改,它将创建一个新的Billy Bob记录(重复)和相应的相关表。 / p>

        intObj.FirstName = "Billy";
        intObj.Lastname = "Bob";
        intObj.Important = 100;
        intObj.LastSeen = DateTime.Now.Date;
        intObj.Cars = new List<Car>{
            new Car{
                Model = "Commodore",
                Make = "Holden",
                YearMade = DateTime.Today.Date,
                Odometer = 15000,
                EmailWordCount = 500,
                TyreStatuss = new List<TyreStatus>{
                    new TyreStatus{
                        Tyre1 = "Good",
                        Tyre2 = "Good",
                        Tyre3 = "Okay",
                        Tyre4 = "Okay"
                        }                                 
                },
                Services = new List<Service>{
                    new Service{
                        Cost = "$500",
                        Time = "2 Days",
                        Date = DateTime.Today
                    }
                },
            }
        };

谢谢

1 个答案:

答案 0 :(得分:4)

在以下片段中,您有Employee类,它引用了另外两个实体:Assignment和一个Country的集合。
像EF,NHibernate等ORM有一个称为传递持久性的特性,也就是说,如果一个对象(赋值和国家)被一个持久的(Employee)引用,那么赋值和国家最终也将成为持久性的,在你的EF案例中,SaveChanges方法在Context中被调用,而你没有明确地保存它们。

    public class Employee
        {
            public virtual Guid Id { get; protected set; }
            public virtual string EmployeeNumber { get; set; }
            public virtual Country BirthCountry { get; set; }
            private ICollection<Assignment> _assignment = new List<Assignment>();
            public virtual ICollection<Assignment> Assignments
            {
                get
                {
                    return _assignment;
                }

                set
                {
                    _assignment= value;
                }
            }
        }

        public class Assignment
        {
            public virtual Guid Id { get; protected set; }
            public virtual DateTime BeginTime { get; set; }
            public virtual DateTime EndTime { get; set; }
            public virtual string Description{ get; set; } 
        }

        public class Country
        {
            public virtual Guid Id { get; protected set; }
            public virtual string Name { get; set; }
        }

   //Somewhere in your program      
   private void SaveAllChanges()
         {
             _db = new EFContext();
             //Creating a new employee here, but it can be one loaded from db
             var emp = new Employee { FirstName = "Emp Name", 
                 LastName = "Emp Last", EmployeeNumber = "XO1500"
             };
             emp.BirthCountry = new Country { Name = "Country1" };
             emp.Assignment.Add(new Assignment{ BeginTime = DateTime.Now,EndTime=DateTime.Now.AddHours(1) });

            //Only employee is explicitly added to the context
            _db.Employees.Add(emp); 
            //All the objects in the employee graph will be saved (inserted in this case) in the db. 
            _db.SaveChanges();
        }
    }

编辑:

这与我上面的代码非常相似,一次是&#34; Billy Bob&#34;创建你只需要更新它,包括他购买的任何新服务;

伪代码:

var bob = _db.Clients.SingleOrDefault(c=> c.Id = "Bob Row Id")
//Bob buy a car:
bob.Cars.Add(new Car()...)
//...and change tire 1 from an old car
var car = bob.Cars.SingleOrDefault(c=> c.Id = "Car Row Id")
car.TireStatus.Tire1 = "New"
....

//Persist all changes
//Existing objects will be updated..., and the new ones created in this process will be inserted
_db.SaveChanges()

如果这澄清了你的想法,请告诉我