I did try to make sample by EF5 and MySQL.
[Table("agency")]
class Agency
{
[Key]
[Required]
public int agency_id { get; set; }
public string name { get; set; }
}
class MyContext : DbContext
{
public DbSet<Agency> Agency { get; set; }
}
class Sample
{
public static void Main()
{
using (var db = new MyContext())
{
var query = from x in db.Agency select x.agency_id;
var new_num = query.DefaultIfEmpty<int>().Max(p => p == null ? 0 : p);
new_num++;
Agency a = new Agency
{
agency_id = new_num,
name = "Test"
};
db.Agency.Add(a);
db.SaveChanges();
}
}
}
At first. It was successful.
But at second time, It threw the exception in db.SaveChanges().
{"Duplicate entry '0' for key 'PRIMARY'"}
With the thrown exception, I traced variable.
agency_id had '1', certainly.
What was wrong?
Update 1
Update 2
!!!!???????????????????????????