LINQ / MySQL / AutoIncrement
Posted: Mon 06 Jun 2011 09:16
Hello,
With the following code:
The Id field won't have auto increment set in the MySQL database. When I attempt to insert an object an exception is thrown saying that Id doesn't have a default value. I want MySQL to assign the value automatically.
The only solution I could think of was to use a MySqlCommand to alter the table after the database is created - is there a better way?
Thanks,
Paul
With the following code:
Code: Select all
[Table]
public class Customer
{
[Column( IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert )]
public int Id { get; set; }
...
}
public class Database
{
public static void Create( DataContext context )
{
if( !context.DatabaseExists() )
{
context.GetTable();
context.CreateDatabase();
context.SubmitChanges();
}
}
}
The only solution I could think of was to use a MySqlCommand to alter the table after the database is created - is there a better way?
Thanks,
Paul