2 Replies - 1433 Views - Last Post: 17 April 2013 - 07:45 AM Rate Topic: -----

#1 BattlFrog  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 09-April 12

Retrieve id from mysql db with entity framework after model is saved

Posted 15 April 2013 - 12:47 PM

Building an asp mvc3 app with razor, c#, EF 4.4, MySql and .net 4.0

Usually when I build an mvc app with sql server, my controller will look something like:

Public ActionResult Save(Model model)
{
	Var db = new MyEntities();
	db.myTable.add(model);
	db.SaveChanges();
	int myid = model.id;
	return View();
}



Using this method, entity framework does some magic and the myid in the second to last line above, will have the id that was just created in the database for the new row, assuming I have the id column set as an auto incrementing IDENTITY.

I tried this in my current project with MySql, with the ID field defined as auto_increment in the database, but myid value is 0.

I have mysql references in the project:

MySql.Data
MySql.Data.Entity
MySql.Web

I am using mysql on this because where I am hosting it, the options are mysql or sql server express 2008 with the 4 gb limit, and I will blow past 4gb with this site, in about 3 months.

If this is not a feature the EF can do with MySQL, let me know and I will look into using my backup host company, which uses SQL Server 2012 express, which has a 10gb limit.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Retrieve id from mysql db with entity framework after model is saved

#2 MathewS  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 343
  • Joined: 14-May 02

Re: Retrieve id from mysql db with entity framework after model is saved

Posted 17 April 2013 - 02:42 AM

It might be an issue with Entity Framework. Can you open your EDMX, and check that the ID field that the property StoreGeneratedPattern is set to Identity. This just lets EF know that the DB will handle the ID generation.

This post has been edited by MathewS: 17 April 2013 - 02:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Retrieve id from mysql db with entity framework after model is saved

Posted 17 April 2013 - 07:45 AM

Your model object does not have access to the id until you ask for it from the db after it has been saved.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1