Bug #61601 Entity Framework attach() method does not update the database.
Submitted: 22 Jun 2011 22:16 Modified: 28 Jun 2011 21:11
Reporter: Mike Hillyer Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.4.1 OS:Windows
Assigned to: Julio Casal CPU Architecture:Any
Tags: Connector/Net, entity framework

[22 Jun 2011 22:16] Mike Hillyer
Description:
The attach method, used to create a relationship between two objects in the Entity Framework, does not insert into a joining table in MySQL when called. 

No errors are thrown, the relationship is just not inserted into the joining table in the database.

As an example:

ramblrEntities _db = new ramblrEntities();

user me = (from u in _db.users
             where u.user_id == user_id
             select u).Single();

place dest = (from p in _db.places
             where p.place_id == place_id
             select p).Single();

me.liked_places.Attach(dest);

_db.SaveChanges();

How to repeat:
Create a simple schema of three tables, where two represent entities and one is a joining table with just the primary keys of the two tables.

Create a model from the schema and use code similar to the above, pulling one entity from each table and then call the attach method of the one entity's collection of the other.

Check the joining table for changes.

Suggested fix:
Implement the attach method.
[23 Jun 2011 18:52] Richard Deeming
I think you're using the wrong method. Try "me.liked_places.Add(dest);" instead.
[26 Jun 2011 1:44] Mike Hillyer
As you can see from my example, both entities already exist.

.add() is used when the object in the collection does not previously exist, whereas .attach() is used to create a relationship between two entities that already exist.

See http://msdn.microsoft.com/en-us/library/bb896271.aspx
[26 Jun 2011 2:52] Mike Hillyer
It appears my source was wrong, the add method worked. My apologies.
[28 Jun 2011 21:11] Julio Casal
Thank you for taking the time to write to us, but this is not a bug. 

Attach will not insert new records in a joining table. Please use Add to get the expected result.