c# - get object ID after insert in database in asp.net MVC with oracle databse -


please, need retrieve id(primary key) of object have inserted in oracle database, many suggestion said:

 context.entity.add(myobject);  context.savechanges();  int x=myobject.id; 

but didn't work. when tries print x in following statement in controller:

return view(x.tostring()); 

(since can't print value in way), receive value "0", when open database found value id (not "0") oracle database has sequence , trigger assign id myobject. don't know problem is, in database? or compatibility problem between database , mvc? thank in advanced:)

the problem on model. ef needs know field in question set in db.

in model, add databasegenerated option this:

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

the options can used databasegeneratedoption computed (generated on insert , update), identity (generated on insert), , none (not generated).

once this, ef knows value db after save. otherwise, assumes db isn't touching values send.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -