Prevent EF from trying to update/insert child objects

entity-framework entity-framework-6


I have an Address object that has a City property. When creating a brand new Address to be inserted via EF6, I fill all required basic Address properties (address line 1, postal code, etc), but I don't need a fully hydrated City instance so long as it has the ID like so:

address.City = new City { Id = 1 };

When I attempt to insert my Address, it also attempts to do validation on the City's properties, but I don't want to do any CRUD on City since all I need is its ID.

I found the below question that introduced me to detaching entries from the DbContext so that EF does not attempt to do CRUD on said objects:

How do I stop Entity Framework from trying to save/insert child objects?

What seems to be happening when I detach the City instance is that it also nulls it out, so my Address has a null City property. This is a problem because City is also required, so a DbEntityValidationException is thrown saying "The City field is required".

I am new to EF, so perhaps the way I am going about all of this is just wrong to begin with.

Edit By request, here's all my code:

Building my Address entity in my client before passing it to WebApi endpoint:

var user = new AppUser { Id = 1 };
var address = new Address
    City = new City { Id = 277 },
    Line1 = "123 whatever ln",
    PostalCode = "01233",
    CreatedBy = user,
    ModifiedBy = user,
    CreatedOn = DateTime.Today,
    ModifiedOn = DateTime.Today

In my ASP.NET app, I create an array of instances I want to detach from the context:

Detached = new object[] {

Just before the save, I detach all instances in the array:

foreach (var d in DetachedObjects)
    dbContext.Entry(d).State = EntityState.Detached;

What I thought was happening with detaching properties was that it was simply telling EF not to do any CRUD on them, but I didn't want it to null them out because I want the parent/principle entity to have the ID for its FK.

Here are my Address and City classes:

public class Address : CisEntity
    public string Line1 { get; set; }

    public string Line2 { get; set; }

    public City City { get; set; }

    public string PostalCode { get; set; }

[DebuggerDisplay("{Name}, {Province.Name}, {Province.Country.Name}")]
public class City : CisEntity, IEntityName
    public Province Province { get; set; }

    public string Name { get; set; }
5/23/2017 12:26:00 PM

Popular Answer

As far as I know, you have two options.

Option 1)

Null all the child objects, this will ensure EF not to add anything. It will also not delete anything from your database.

Option 2)

Set the child objects as detached from the context using the following code

 context.Entry(yourObject).State = EntityState.Detached

Note that you can not detach a List/Collection. You will have to loop over your list and detach each item in your list like so

foreach (var item in properties)
     db.Entry(item).State = EntityState.Detached;
8/22/2014 8:47:37 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow