0

I am using Entity Framework and have a connection to a MySQL database. The id column is set to use StoreGeneratedPattern Identity and the column in the database has been set to auto-increment. When I create a new object and save it to the database, the item posts correctly in the database. However, after saving, the id of the object in C# remains 0 rather than reflecting the value than was assigned by the database.

The section of code is given below:

Group newGroup = new Group("MyGroupName", "Active");
dbContext.Groups.Add(newGroup);
dbContext.SaveChanges();
int testId = newGroup.id;

Even though "newGroup" saves in the database with a database-assigned id, when I read the id (such as I do when reading testId) the id is still 0.

Based on this, I have tried adding

dbContext.Entry(newGroup).Reload();

after SaveChanges() and I have also tried (based on this and this) adding

var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
objectContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, newGroup);

after SaveChanges() in an attempt to refresh the object (and thus the id) from the database, yet the problem remains. How can I get the id that was assigned by the database?

EDIT: Adding class definition for Group:

[Table("groups")]    
public partial class Group
{
    public Group()
    {
        this.user_groups = new HashSet<UserGroup>();
    }

    public long id { get; set; }
    public string name { get; set; }
    public string status { get; set; }
    public System.DateTime created_at { get; set; }
    public System.DateTime updated_at { get; set; }

    public virtual ICollection<UserGroup> user_groups { get; set; }
}
Community
  • 1
  • 1
Stanley
  • 5,261
  • 9
  • 38
  • 55
  • 1
    Entity Framework refreshes based on the id, if it doesn't get back the correct id no amount of refreshing can work. Could you post your Group class and any mappings you might have. BTW, I have looked at the source code for the MySQL EF adapter and quite frankly it is unusable. I would HIGHLY recommend AGAINST using MySQL with Entity Framework. – Aron Oct 01 '13 at 10:33
  • I have added the class definition for Group – Stanley Oct 01 '13 at 10:42

3 Answers3

0

Try decorating your id with the [Key] attribute.

ESG
  • 8,988
  • 3
  • 35
  • 52
0

It SHOULD be this attribute

[DatabaseGenerated(DatabaseGenerationOption.Identity)]

However, this SHOULD be the default.

The [Key] attribute, should be unnecessary since the column name Id is magical... Although this might only be the case when using the accepted naming convention for C#.

I wonder if it might be the long that your id property is typed, or possibly the naming convention... you could try naming it Id.

Aron
  • 15,464
  • 3
  • 31
  • 64
0

I'm having the same problem with my project. What I did for a work around was to order the table (group) by the ID descending and select the first or default record, then select the ID column.

var newID = dbcontext.Groups.OrderByDescending(x => x.id).FirstOrDefault().ID

You can then assign that to whatever you need and save changes again. I know it's an old thread but hopefully this helps. Seems like there should be a better way to do it...

Auzi
  • 337
  • 3
  • 13