views:

7074

answers:

6

Is the Entity Framework aware of identity columns ? I am using sql server 2005 express edition and have several tables where the primary key is an identity column, when I use these tables to create an entity model and use the model in conjunction with an entity datasource bound to a formview in order to create a new entity I am asked to enter a value for the identity column. Is there a way to make the framework no ask for values for idenity columsn ?

+2  A: 

Entity Framework is aware and can handle identity columns.

Your problem can be maybe not the EF itself but the generated formview of it. Try to delete the input for the identity column from the insert form and let's see what happens.

Biri
+2  A: 

You should set the identity columns' identity specification so that the (Is Identity) property is set to true. You can do this in your table designer in SSMS. Then you may need to update the entity data model.

Perhaps that what you mean by saying the "Primary key is an identity column," or perhaps you missed this step.

YeahStu
A: 

If all else fails before you rip out your hair - try deleting your EntityModel and re-importing from SQL Server. If you've been tweaking the keys and relationships and relying on the 'update model from database' function it's still a bit buggy in the RC version I've found - a fresh import may help.

scotta
+15  A: 

I know this post is quite old, but this may help the next person arriving hear via a Google search for "Entitiy Framework" and "Identity".

It seems that Entity Frameworks does respect server-generated primary keys, as the case would be if the "Identity" property is set. However, the application side model still requires a primary key to be supplied in the CreateYourEntityHere method. The key specified here is discarded upon the SaveChanges() call to the context.

The page here gives the detailed information regarding this.

biozinc
This is just what I was looking for. Thanks!
Mark Good
+1  A: 

Entity framework does not fully understand Identities for some reason. The correct workaround is to set the Setter for that column to Private. This will make any generated UI understand that it should not set the identity value since it is impossible for it to set a private field.

In this case, you wont be able to edit the entity via the key, since the value will be 0 for the updated object and you wont be able to save the upadted changes as you cant find the object in the collection via the key. (this case is for MVC)
Chinjoo
A: 

I don't know if this still helps, but I have started writing a simple tutorial on the Entity Framework. You can find it here if in need: Entity Framework Tutorial

Daniel