Prevent EF from trying to update/insert child objects

entity-framework entity-framework-6

Question

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[] {
    value.Principle.ModifiedBy,
    value.Principle.CreatedBy,
    value.Principle.City
};

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

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

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:

[DebuggerDisplay("{Line1}")]
public class Address : CisEntity
{
    [MaxLength(200)]
    [Required]
    public string Line1 { get; set; }

    [MaxLength(200)]
    public string Line2 { get; set; }

    [Required]
    public City City { get; set; }

    [MaxLength(25)]
    public string PostalCode { get; set; }
}

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

    [MaxLength(100)]
    public string Name { get; set; }
}
1
2
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;
}
52
8/22/2014 8:47:37 AM


Related Questions





Related

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