Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ADO.NET Entity Framework and identity columns

People also ask

Can we use Entity Framework and ADO.NET together?

EF is built on top of ADO.Net, meaning that you can use both at the same time.

What is the difference between ADO and Entity Framework?

Entity framework is ORM Model, which used LINQ to access database, and code is autogenerated whereas Ado.net code is larger than Entity Framework. Ado.net is faster than Entity Framework. 1. ADO.Net is create bunch of data layer code, EF is not create.

How do I get the identity column value after insert in Entity Framework?

EF execute each INSERT command followed by SELECT scope_identity() statement. SCOPE_IDENTITY returns the last identity value inserted into an identity column in the same scope. The above example will execute the following SQL in the database. WHERE @@ROWCOUNT = 1 AND [StudentID] = scope_identity();


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.


If you are using Entity Framework 5, you can use the following attribute.

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

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.


This is the best answer I've seen. You have to manually edit the storage layer xml to set StoreGeneratedPattern="Identity" on each primary key of type UniqueIdentifier that has the default value set to NewID().

http://web.archive.org/web/20130728225149/http://leedumond.com/blog/using-a-guid-as-an-entitykey-in-entity-framework-4/


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.