Wednesday, May 28, 2014

Add ID value for non-auto-increament field under Entity Framework

If you use an “ID” field in your entity, & this “ID” field is not auto-increament, Entity Framework will fail when generate insert sql.

Solve:

Add a DatabaseGeneratedAttribute to this “ID” field, with DatabaseGeneratedOption.None option, like this:

public abstract class BaseAccount{    [Key]    [DatabaseGenerated(DatabaseGeneratedOption.None)]    public int ID { get; set; }    ......}

Successfully solved under Entity Framework 4-6.

Entity Framework 6.0 MigrationHistory table on SQLite

The following SQL is used to create __MigrationHistory table in SQLite database for Entity Framework 6.0 in Code-First mode.

CREATE TABLE [__MigrationHistory] ([MigrationId] CHAR NOT NULL, [Model] BLOB NOT NULL, [ContextKey] CHAR NOT NULL, [ProductVersion] CHAR NOT NULL, CONSTRAINT [sqlite_autoindex___MigrationHistory_1] PRIMARY KEY ([MigrationId]));