I am trying to use GraphDiff to update a hierarchical entity graph.
I have my Availability which has a number of Items, and also a booking which eventually wants to select some of these.
public class Availability{
//...
public virtual List<Item> Items { get; set; }
//...
}
public class Booking{
//..
public virtual List<Item> Items {get;set;}
//..
}
public class Item{
//..
public int ID {get;set;}
//..
}
public class MyContext : DbContext{
protected override void OnModelCreating(DbModelBuilder modelBuilder){
modelBuilder.Entity<Booking>().HasMany(b => b.Items).WithOptional(e => e.Booking).WillCascadeOnDelete(false);
modelBuilder.Entity<Availability>().HasMany(ar => ar.Items).WithOptional(e => e.Availability).WillCascadeOnDelete(false);
}
}
what I am trying to achieve is, rather than - when I assign an Item to a booking form the availability - EF generating 2 records, it generates a single record with 2 foreign keys in it
the DB table does have 2 foreign key columns in it - and I am updating like this
public ActionResult SaveItems(Availability model){
Booking b = db.Bookings.Include(bk=>bk.Items).Include(...).singleOrDefault(bk=>bk.ID == model.ID);
b.Items = model.Items;
db.UpdateGraph(b, map => map
.OwnedCollection(bk=>bk.Items, with => with
.OwnCollection(i=>i.ChildList)));
db.SaveChanges();
}
I am assuming that there is something in the fluent mappings that may be able to make this work, but I cant seem to work it out
I have also tried adding this to the OnModelCreating to no avail.
modelBuilder.Entity<Item>().HasOptional(e => e.Booking).WithMany(b => b.Items).WillCascadeOnDelete(false);
modelBuilder.Entity<Item>().HasOptional(e => e.Availability).WithMany(a => a.Items).WillCascadeOnDelete(false);
is this even possible ?
Aucun commentaire:
Enregistrer un commentaire