Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entityframework duplicating when calling savechanges

I am using entityframework 5 code first, I have a model like this.

class Product {
    public Product() {
        Fabrics = new BindingList<FabricLineItem>();
    }
    ...
    public virtual ICollection<FabricLineItem> Fabrics { get;set; }
}

class FabricLineItem {
    [ForeignKey("Fabric")]
    public int FabricId { get; set; }  
    public virtual Product Product { get;set; }
    public virtual Fabric Fabric { get;set; }  
}

class Fabric {
    ...
}

I already have fabrics in my database. I create a new product object and add some fabriclineitems to the collection. When I try to save product what happens is it duplicates the fabric in the database and reference it to the new one after calling

 DataContext.SaveChanges(); 

Before the call to savechanges the values in the debugger are correct by after calling they are changed ? Any idea why I am getting this strange behavior ?

like image 771
Ahmed Avatar asked Mar 13 '13 19:03

Ahmed


People also ask

How do you avoid duplicates in Entity Framework?

Duplicate rows in Entity Framework SQL View Entity Framework auto set fields as entity key for those not null column and return the row that match those entity key that causes the problem. You can set AsNoTracking option directly on your view to resolve this issue.

What does the DbContext SaveChanges () method return?

What does the DbContext SaveChanges () method return? Returns. The number of state entries written to the underlying database. This can include state entries for entities and/or relationships.

Does SaveChanges commit?

In Entity Framework, the SaveChanges() method internally creates a transaction and wraps all INSERT, UPDATE and DELETE operations under it. Multiple SaveChanges() calls, create separate transactions, perform CRUD operations and then commit each transaction.

What is the return type of SaveChanges in Entity Framework?

SaveChanges() always returns 0 – Entity Framework According to EF documentation, SaveChanges() should return a number of affected rows.


1 Answers

I was also having the problem of E.F. not recognising that an item already existed when it was referenced within another new object to be added to the DB.

@NeilThompson gave me the clue above how to solve this:

You need to attach an existing entity to the context so that it knows that it already exists:

For example:

context.Fabrics.Attach(product.Fabric); 
context.Products.Add(product);
context.SaveChanges();

return organisation;

I got this from here:

"If you have an entity that you know already exists in the database but which is not currently being tracked by the context then you can tell the context to track the entity using the Attach method on DbSet. The entity will be in the Unchanged state in the context."

http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx

like image 181
Holland Risley Avatar answered Oct 07 '22 05:10

Holland Risley