Bug #61902 Inserting object using Entity Framework does not update id member
Submitted: 18 Jul 2011 21:44 Modified: 1 Aug 2011 20:13
Reporter: Matt Houser Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.4.3 OS:Any
Assigned to: Julio Casal CPU Architecture:Any

[18 Jul 2011 21:44] Matt Houser
Description:
When using Entity Framework and the Connector/NET, when inserting an entity into you entity set, SaveChanges() does not update the entities id.

How to repeat:
1.  Create a MySQL Model (single table with auto increment int primary key) and forward engineer to a database.
2.  Using Visual Studio 2010, create an ADO.NET Entity Data Model based on your database.
3.  In C#, use your data model to insert a new object.  For example:

MyEntities ent = new MyEntities();

MyTableObject o = new MyTableObject();
o.Data = "Some Data";

ent.MyTableObjects.AddObject(o);

ent.SaveChanges();

if (o.MyTableObjectId == 0)
  Console.WriteLine("Incorrect");
else
  Console.WriteLine("New Id: {0}", o.MyTableObjectId);

Suggested fix:
When saving a new record, update the object's primary key id (if auto increment).
[19 Jul 2011 18:54] Matt Houser
Sample application and database model

Attachment: EFFK.ZIP (application/x-zip-compressed, text), 13.84 KiB.

[19 Jul 2011 18:59] Matt Houser
Sorry, the above EFFK.ZIP file isn't correct.  Will update with another version.
[19 Jul 2011 19:37] Matt Houser
Demonstration Project and DB Model

Attachment: Problem.zip (application/x-zip-compressed, text), 145.28 KiB.

[19 Jul 2011 19:38] Matt Houser
When forward engineering the model, make sure to include INSERT statements.  s4s_EmailAddresses contains inserts.
[1 Aug 2011 20:13] Julio Casal
Not enough information was provided for us to be able to handle this bug. Please re-read the instructions at http://bugs.mysql.com/how-to-report.php

The Log entity in Smtp4SesModel.edmx included in Problem.zip had StoreGeneratedPattern = None for LogId. To update an entity auto generated property you need to change StoreGeneratedPattern to Identity. In your sample, I removed the Log entity and readded it to the model by choosing "Update Model from Database". That automatically set StoreGeneratedPattern = Identity for LogId and the new Id got autopopulated after SaveChanges.

If you can provide more information, feel free to add it to this bug and change the status back to 'Open'.

Thank you for your interest in MySQL.
[6 Jul 2012 2:26] Lucas Teixeira
Were you able to solve the problem?
I'm going through the same situation
[10 Mar 2014 13:01] Timothy Mapley
For the PK Property (such as ID), make sure StoreGeneratedPattern=Identity