Entity Framework 6 updates the table and inserts into the tables associated with the foreign key

Not sure if you can name the word for this, so feel free to edit if it is inaccurate.

Using an example, what I'm trying to do is update the record in the foo table and then create new records in the next table with the Foo PK tables as a foreign key, think of a one-to-many relationship.

How to update a table with foreign key constraints and create a new related record (s) in these subsequent tables?

I am currently using Entity Framework 6 to add .Add and .Attach to the context and store them in the database.

Edit

To clarify what I'm trying to achieve, the following object is a shortened example that I am trying to keep in context. If I try. Add intObj after “Billy Bob” has already been created, because he bought a new car, another service or his tires changed it, will create a new Billy Bob record (duplicate) and the corresponding related tables.

        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
                    }
                },
            }
        };

thank

+4
source share
1 answer

Employee, : Assignment .
ORM, EF, NHibernate .., , Transitive Persistence, (Assignment and Country) (Employee), Assignments and Country , EF SaveChanges .

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

EDIT:

, "Billy Bob" , .

:

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

, .

+3

All Articles