How to Stop Entity Framework from Trying to Save/Insert Child Objects

Does Entity Framework Core have a simple way of preventing the update of child or parent entities?

What you are asking is quite simple in EF Core. If you don't want EF Core change tracker operation to process the related data, set the EntityEntry.State rather than calling DbContext / DbSet methods like Attach, Add, Update, Remove etc.

This behavior is different from EF6 where methods and setting state are doing one and the same, and is partially mentioned in the Saving Related Data - Adding a graph of new entities
documentation topic:

Tip

Use the EntityEntry.State property to set the state of just a single entity. For example, context.Entry(blog).State = EntityState.Modified.

So in your sample, simply replace

context.Attach(entity);

with

context.Entry(entity).State = EntityState.Unchanged;

Insert a new entity without creating child entities if they exist

Taken from here.

When adding a new entity that has existing child objects (object that exists in the database), if the child objects are not tracked by EF, the child object will be re-inserted. Unless you manually attach the child object first.

Try something like the following to set the child objects state:

public void InsertProduct(Product item)
{
// Calling this code before the context is aware of the Child
// objects will cause the context to attach the Child objects to the
// context and then set the state.
// CustomerContext.Entry(childitem).State = EntityState.Unchanged
CustomerContext.Entry(item.ChildObject).State = EntityState.Modified;

CustomerContext.Entry(item).State = EntityState.Added;
CustomerContext.Set<Product>().Add(item);
}

Entity Framework appears to be saving children objects before the parent object

EF does work out the correct order based on the navigation properties. Something else is wrong. A few things might be the issue.

Why are you using [DatabaseGenerated(DatabaseGeneratedOption.None)]? Are you creating and maintaining the ids or is the database? If its the database don't you want [DatabaseGenerated(DatabaseGeneratedOption.Identity)]?

Also a nullable primary key on the agent? Is that correct? Primary keys can't/shouldn't be nullable...

You shouldn't need to set [InverseProperty("Office")] for a normal 1 to many relationship. This might be confusing EF.

C# Entity Framework: Insert Child Entity and Update Parent, One Transaction

Entity Framework automatic scaffold was setup.
So the following worked,
Instead of using employee.ProductSaleId, just use ProductSale, and update the Parent directly.

productSale = new ProductSale { Product = "Furniture", Amount= 1000, EmployeeId = 5};
employee.ProductSale = productSale;
_dbContext.SaveChanges();

Entity Framework Core will automatically adjust with Insert or Update

Entity Framework Core: Get parent with all child entities where exists a child with specific criteria

You can use Any():

_dbContext.Parents
.Include(p => p.Children)
.Where(p => p.Children.Any(c => String.Equals(c.Type, "Alpha")))
.ToList(); // or use await and ToListAsync()

So it queries parents (including children) that has at least one child with type equal to Alpha.



Related Topics



Leave a reply



Submit