public void AddEvent(Event e) { using (var context = new EventkalenderContext(xmlPath)) { context.Event.Add(e); context.SaveChanges(); context.Entry(e).Reference(temp => temp.Nation).Load(); context.Entry(e).Collection(temp => temp.Persons).Load(); } }
public void UpdatePerson(Person p) { using (var context = new EventkalenderContext(xmlPath)) { Person dbPerson = context.Person.Include(temp => temp.Events).SingleOrDefault(temp => temp.Id == p.Id); if (dbPerson == null) { return; } List <Event> addedEvents = p.Events.Except(dbPerson.Events).ToList(); List <Event> deletedEvents = dbPerson.Events.Except(p.Events).ToList(); deletedEvents.ForEach(c => dbPerson.Events.Remove(c)); foreach (Event e in addedEvents) { // Set fields to null to avoid circular references e.Nation = null; e.Persons = null; DbEntityEntry eventEntry = context.Entry(e); if (eventEntry.State == EntityState.Detached) { context.Event.Attach(e); } dbPerson.Events.Add(e); } context.SaveChanges(); } }
public void AddPerson(Person p) { using (var context = new EventkalenderContext(xmlPath)) { context.Person.Add(p); context.SaveChanges(); context.Entry(p).Collection(temp => temp.Events).Load(); } }
public void AddNation(Nation n) { using (var context = new EventkalenderContext(xmlPath)) { context.Nation.Add(n); context.SaveChanges(); context.Entry(n).Collection(temp => temp.Events).Load(); } }