June 15, 2011

Server-generated keys and server-generated values are not supported by SQL Server Compact

Errors:
{"Server-generated keys and server-generated values are not supported by SQL Server Compact."}
{"An error occurred while updating the entries. See the inner exception for details."}

Problem:
The current version of Entity Framework does not support identity keys using SQL Compact ;-(

Solution:
1- Add the Guid.NewGuid() after new row like below here (for uniqueidentifier ID)

BindingListCollectionView CurrentView = categoriesViewSource.View as BindingListCollectionView;
Table tb1 = CurrentView.AddNew() as Table1;
tb1.ID = Guid.NewGuid();
this.CurrentView.CommitNew();

2- Another solution can be find the last and greatest ID and make a new one with ++ to that like here (for int ID)

3- Another solution can be used of  DateTime.UtcNow.Ticks (for bigint ID)

PS: There's a little problem with True value for Identity property in SQL CE Table and must be set to False.

2 comments:

وحيد نصيري said...

NHibernate just works fine with SQL-CE! :)

Amastaneh said...

@Vahid: A BIG THANK YOU goes to vahid. The NHibernate is another solution ;-)