Tuesday, April 20, 2010

Solutions to: “Server-generated keys and server-generated values are not supported by SQL Server Compact” with Entity Framework

SQL Compact does not support IDENTITY keys when used with Entity Framework, since the SQL Compact engine only supports a single statement per batch.

This can be solved in various ways:

You can have all your primary keys be of type uniqueidentifier, and assign the NewGuid() value to the object upon creation:

 Employee employee = new Employee();

employee.Id = Guid.NewGuid();

Or you can create an extension method as described here, noting the limitations of this approach.

1 comment:

Andrew said...

FYI - I think the updated link for the extension method is http://enigmadomain.wordpress.com/2010/01/06/sql-compact-identity-columns-and-entity-framework/