Entity Framework: context.AddObject without adding wizards

people,

I have a problem when I create a new object with a master. The problem is that EF is also trying to create the main object, but I don't need it.

My two poco classes look like this:

[DataContract(IsReference = true)]
public class Application
{
    [DataMember]
    public int ID { get; set; }

    [DataMemeber]
    public int ProjectManagerId {get; set; }

    [DataMember]
    public ProjectManager PM { get; set;}
}

[DataContract(IsReference = true)]
public class ProjectManager
{
    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string FullName { get; set; }

}

      

When I create a new object in an ASP.NET MVC page, I have a class object Application

with ProjectManagerId equal to 1 and PM fields with ID = 0 and FullName, such as "Forest Gump".

So when I add an object I have an exception that application.PM.ID cannot be 0:

context.Applications.AddObject(application); 
context.SaveChanges();

      

Is it possible to add an obobject without adding a wizard?

I found a workaround, but I don't like it: it assigns the PM field to null before adding the object to the context

application.PM = null;

      

+3


source to share


1 answer


EF has one basic rule that you should be aware of. It works with an entire object graph and does not allow you to combine separate and attached objects on the same object graph. This means that both methods Attach

and AddObject

will always execute on all objects in the object graph (it will intersect your navigation properties and perform the operation recursively).

Because of this essential behavior, you must handle existing objects manually. You have three options:

Don't use the navigation property. Your class Application

has ProjectManagerId

. You only need to set this property to an ID

existing creche without populating the navigation property ProjectManager

to build the relationship.

var application = new Application { ProjectManagerId = 1 };
context.Applications.AddObject(application);
context.SaveChanges();

      



Attach a parent, add a child, and only then establish a connection between them:

// Create attached existing project manager
var projectManager = new ProjectManager { ID = 1 };
context.ProjectManagers.Attach(projectManager);

// Create a new added application
var application = new Applications();
context.Applications.AddObject(application);

// Now you are making relation between two entities tracked by the context
application.ProjectManager = projectManager;  
context.SaveChanges();

      

The last option is to simply capture the state of existing objects. In this case, you will install the project core unchanged, while the application still remains in the added state:

// Add application and its related project manager
context.Applications.AddObject(application);
context.ObjectStateManager.ChangeObjectState(application.ProjectManager, EntityState.Unchanged);
context.SaveChanges();

      

+4


source







All Articles