I'm using Entity Framework 4.4 and I have a One-To-Many relationship model like this:
class Item {
public string keyPart1 { get; set; }
public string keyPart2 { get; set; }
public virtual Container container { get; set; }
public string ContainerId { get; set; }
}
// Idea is that many Items will be assigned to a container
class Container {
public string ContainerId { get; set; }
private ICollection<Item> _Items;
public virtual ICollection<Item> As
{
get { return _Items ?? (_Items = new HashSet<A>()); }
protected set { _Items = value; }
}
}
Now, here's the DbContext:
public class StorageContext : DbContext
{
public DbSet<Item> Items { get; set; }
public DbSet<Bucket> Buckets { get; set; }
public override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Item>().HasKey( i => new { i.keyPart1, i.keyPart2 } ).HasRequired( i => i.Container );
}
}
Now, supposing I have N Item instances. Each Item belongs to a container, which contains multiple item instances, each of which belongs to a container, and so the model recurses endlessly.
I want to then cycle through my current list of Item instances and add each to the db context:
foreach (var i in LocalItemList)
{
IDbSetExtensions.AddOrUpdate<Item>(db.Items, i);
}
dbContext.SaveChanges();
The problem that I can't figure out is how to tell the context to AddOrUpdate the Container so that I don't get primary key duplicate exceptions. At some point we'll run into an Item that has the same Container as another, but I'll get a duplicate primary key exception on SaveChanges().
If I Add a Container to the DbSet, are the Items added to the Set as well? How can I make that an AddOrUpdate instead?