C # Integer Primary Key Generation Using Entity Framework with Local Database File (Sdf)

I am writing a standalone application and I thought using the Entity Framework to store my data.

Currently, the application is small, so I can use the local database file to start.

The fact is that the local database file does not have the ability to automatically generate whole primary keys, as SQL Server does.

This is not a problem defining the identifier column as “identifying” when creating the table, but when trying to call the SaveChanges method, it throws the following exception:

{"Server keys and server-generated values ​​are not supported by SQL Server Compact."}

Any suggestions on primary key management for objects in the local database file that will be compatible with SQL Server in the future?

Thanks, Ronny

+5
source share
1 answer

There are three general methods that I can think of when the database does not have an automatic number.

1) First enter MAX (ID_column) +1 to get the next ID value. However, you should be aware of issues with multiple users. In addition, these numbers are not disposable. If you delete the line and add a new line, you will get the same identifier. This may or may not be a problem for you.

2) GUID. , .

3) , , . , , .

+2

All Articles