Auto increment, C#, entity framework

c# entity-framework

Question

Under VC# 2010, I'm studying Entity Framework.

For learning reasons, I created a simple table with one field of the "id" type with the identity set to true. From that table, I created an entity data model and linked it to a datagrid view. The issue is that each added row needs to have an id of 0, not auto-increment (which is impossible of course, since id must be unique)

Why am I misusing this? How do I set up the SQL database or EF?

1
25
9/25/2016 10:42:55 AM

Accepted Answer

Verify that the StoreGeneratedPattern attribute of the autoincrement field in your EDMX model is set to "Identity." As a result, EF is aware that the DB manages the autonumbers.

This is best stated here: With Entity Framework, autonumber

38
5/23/2017 11:54:39 AM

Popular Answer

Simply adding to the entity collection does not set and increment the identity. Before you call context, the object is not really stored to the database. SaveChanges()...

db.AddToUserSet(user);//Added to EF entity collection
db.SaveChanges();//INSERT executed in db, Identity set and incremented.


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow