c# - Mapping and metadata information could not be found for EntityType /Custom model -


i'm trying create custom model registration , want save form info database. worked when used user model user model auto generated model , don't want rewrite every time decide make changes. when did custom register model inherit user model getting error when try register new user.

here register model:

using system; using system.collections.generic; using system.componentmodel.dataannotations; using system.componentmodel.dataannotations.schema; using system.linq; using system.web; using creckjackmvc;  namespace creckjackmvc.models {  public  class register : user {     [key, databasegenerated(databasegeneratedoption.identity)]//important auto user increment     new public decimal cj_user_id { get; protected set; }       [required(errormessage = "please provide full name", allowemptystrings = false)]     new public string cj_user_name { get; set; }      [required(errormessage = "please provide email", allowemptystrings = false)]     [regularexpression(@"^([0-9a-za-z]([\+\-_\.][0-9a-za-z]+)*)+@(([0-9a-za-z][-\w]*[0-9a-za-z]*\.)+[a-za-z0-9]{2,3})$",                 errormessage = "please enter valid email")]     new public string email { get; set; }      [required(errormessage = "please provide password", allowemptystrings = false)]     [datatype(system.componentmodel.dataannotations.datatype.password)]     [stringlength(50, minimumlength = 8, errormessage = "password must 8 char long")]     new public string user_pass { get; set; }      [system.web.mvc.compareattribute("user_pass", errormessage = "confirm password not match")]     [datatype(system.componentmodel.dataannotations.datatype.password)]     new public string confirm_user_pass { get; set; }  } } 

here controller:

public actionresult register(creckjackmvc.models.register u)     {         if (modelstate.isvalid)         {             using (creckjackliveentities dc = new creckjackliveentities())             {                  //you should check duplicate registration here                  dc.users.add(u);                 dc.savechanges();                 modelstate.clear();                 u = null;                 viewbag.message = "registration successful! welcome creakjack! ";             }         }         return view(u);     } 

here error msg: enter image description here

you register model should not inherit user. instead need view model representing want display/edit in view (see what viewmodel in mvc)

public  class register {     [required(errormessage = "please provide full name")]     public string username { get; set; }      [required(errormessage = "please provide email")]     [emailaddress(errormessage = "please enter valid email")]`     public string email { get; set; }      [required(errormessage = "please provide password")]     [datatype(datatype.password)]     [stringlength(50, minimumlength = 8, errormessage = "password must 8 char long")]     public string password { get; set; }      [compareattribute("password", errormessage = "confirm password not match")]     [datatype(system.componentmodel.dataannotations.datatype.password)]     public string confirmpassword { get; set; } } 

then in controller

[httppost public actionresult register(register model) {     if (!modelstate.isvalid)     {       return view(model);     }     user user = new user()     {       username = model.username,       password = model.password     }     // save user     // redirect view (e.g. home page) - not return view } 

side notes:

  1. using allowemptystrings = false unnecessary since false default value
  2. use [emailaddress] rather custom regular expression (it generate correct regular expression) need additional checks ensure email not exist (and can use [remote] attribute client side validation
  3. it appear trying save password in database (in plain text), in case dreadful practice. passwords need hashed (and preferably salted) , hashed value stored in database
  4. strongly recommend follow normal pascalcase naming conventions model properties

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -