c# - How to add record into the database using MVC -


i trying add record database showing me error: an exception of type 'system.data.entity.infrastructure.dbupdateexception' occurred in entityframework.dll not handled in user code error when run program, not open view in web-browser

this model:

public class moviemodel {     public int id { set; get; }     public string firstname { set; get; }     public string secondname { set; get; }     public datetime dob { set; get; }     public string type{ set; get; } } 

this controller:

namespace movie.controllers {     public class moviescontroller : controller     {         private applicationdbcontext db = new applicationdbcontext();          //         // get: /movies/         public actionresult addusers(moviemodel model)         {             if (modelstate.isvalid)             {                 db.movies.add(model);                 db.savechanges();             }             return view(model);         }     } } 

second question when up-grading database upgrade-database how specify forgone key? have done via microsoft sql server management?

this occurs when trying add/insert record in object other table. movie table or view?

if table, sure table has primary key doesn't allow duplication.

if not add primary key , update entity framework model , run again.

updates : can create empty action. so, update controller follows

    //     // get: /movies/     public actionresult addstudent()     {         return view();     }        //     // post: /movies/     [post]     public actionresult addstudent(moviemodel model)     {         if (modelstate.isvalid)         {             db.movies.add(model);             db.savechanges();         }         return view(model);     } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -