Search code examples
c#sql-server-cecode-firstguid

SQL Server CE 4.0, CodeFirst and GUID keys


I'm using code-first to design my model and I chose GUID as a data type for my primary keys. My classes looks like this:

public class AccountMovementPlan
{
    [Key, DatabaseGenerated( System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity )]
    public Guid AccountMovementPlanID { get; set; }

    [Required]
    public Guid UserAccountID { get; set; }

    [ForeignKey("UserAccountID")]
    public UserAccount UserAccount { get; set; }     

// etc, etc, etc   
}

Next, I chose SQL Server CE 4.0 as my datastore, implemented configuration to use SqlCEConnectionFactory and use DropCreateDatabaseIfModelChanges as initialization method.

But, when I try to run my app, I'm greeted with an error

An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException occurred in EntityFramework.dll

which is informing me, that

The identity column must be either an integer or big integer data type and cannot be NULL.

My question is obvious: SQL Server CE with code-first doesn't support autogenerated GUIDs as PKs? Or, do I have an error somewhere?

Thanks a lot.


Solution

  • No, that is not supported, you must use Guid.NewGuid() in your code to assign the value.