Prevent EF from trying to update/insert child objects

entity-framework entity-framework-6


I have a City property on an Address object. I fill out all of the basic Address parameters (address line 1, postal code, etc.) when establishing a new Address to be inserted using EF6, but I don't need a completely hydrated City instance as long as it has the ID shown below:

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

It tries to validate the attributes of the City when I try to input my address, however I don't want to perform any CRUD on the City as I just need its ID.

I discovered the following query that taught me how to remove items from theDbContext to prevent EF from attempting to do CRUD on said objects:

How can I prevent Entity Framework from attempting to insert or store child objects?

When I disconnect the City instance, it appears to also null it out, leaving my Address with a null City attribute. City is also necessary, which is a difficulty since aDbEntityValidationException "The City field is necessary," it is tossed.

Since I'm new to EF, it's possible that my approach to everything is inherently flawed.

Edit Here is all of my code as requested:

Before sending it to the WebApi endpoint, I build my Address object in my client:

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

I make an array of the instances I wish to separate from the context in my ASP.NET application:

Detached = new object[] {

I detach each object in the array just before the save:

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

Detaching properties only instructed EF not to do any CRUD operations on them, but I didn't want this to happen since I wanted the parent/principal entity to retain ownership of the ID for its FK.

My Address and City courses are shown below:

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

You have two choices, as far as I'm concerned.

Choice 1

By nullifying every child object, you can make sure that EF doesn't contribute anything. Additionally, nothing from your database will be deleted.

Choice 2)

Use the following code to set the child objects as detached from the context.

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

Keep in mind that you cannot remove aList /Collection You must loop through your list and separately attach each item as follows:

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