Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a new entity without creating child entities if they exist

I'm using EF with code-first and I have models like this:

public class Product
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    public Customer Customer { get; set; }
}

public class Customer 
{
    public Customer ()
    {
        Products = new List<Product>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }

    // more stuff snipped...

    public ICollection<Product> Products{ get; set; }
}

I am receiving a customer ID along with a list of product IDs. When the product doesn't exist in the DB, I want to add it:

    var newProduct = new Product{ Id = id, Name = "<no name yet>", Customer = customer };
    InsertProduct(newProduct);

The problem is that EF tries to cascade the changes and tries to insert a new Customer object, with the same ID as an existing one, so it fails. How do I solve this?

This is the insert method:

    public void InsertProduct(Product item)
    {
        CustomerContext.Entry(item).State = EntityState.Added;
        CustomerContext.Set<Product>().Add(item);
    }
like image 824
Jan van Bloem Avatar asked Mar 02 '15 11:03

Jan van Bloem


1 Answers

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);
}
like image 177
Paul Zahra Avatar answered Nov 06 '22 17:11

Paul Zahra