c# - Entity Framework LINQ Insert -


i'm trying use linq insert object in database. primary key auto increment. need increment id in code or entity framework can it?

i have dbcontext:

travelagencyentities db = new travelagencyentities(); 

i'm trying insert user object:

user newuser = new user();         newuser.firstname = firstname.text.tostring();         newuser.lastname = lastname.text.tostring();         newuser.email = email.text.tostring();         newuser.password = password.text.tostring();          db.users.add(newuser);         db.savechanges(); 

it returns exception:

{"an error occurred while updating entries. see inner exception details."} 

i think error because not set id in object, should auto increment. know problem?

the inner error:

cannot insert explicit value identity column in table 'user' when identity_insert set off 

if use code first, have set following code in dbcontext onmodelcreating.

protected override void onmodelcreating(dbmodelbuilder modelbuilder) {     base.onmodelcreating(modelbuilder);      modelbuilder.entity<user>()         .property(a => a.id)         .hasdatabasegeneratedoption(databasegeneratedoption.none); } 

or set attribute following

public class user {     [key]     [databasegenerated(databasegeneratedoption.none)]     public int id { get; set; }      ..... } 

you must change none identity. identity option default key.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -