Hi!
I am having problems making entity framework save a detached entity that has child entities.
I have a simple One to One entity that has been mapped like this (the child)
This should do it according to various sources I have seen. MasterObject must have one AddedData object and AddedData must have one MasterObjectCode:Me.HasRequired(Function(e) e.MasterObject) _ .WithRequiredDependent(Function(e) e.AddedData) _ .WillCascadeOnDelete(False)
When saving the object I use the ctx.MasterObjects.Add(myMasterObject) and then ctx.SaveChanges()
The problem is that only the MasterObject is saved, and not the AddedData object. As I understand Add() it will set ALL objects in the graph to EntityState = Added
The SaveChanges() works perfect if I do like this:
But the above code looks too bloated and what should I do for One to Many relationships? SHould I loop all objects and set EntityState = Added? Don't think soCode:ctx.Entry(myMasterObject).State = EntityState.Added ctx.Entry(myMasterObject.AddedData).State = EntityState.Added ctx.SaveChanges()
cheers
Henrik



Reply With Quote