c# - WebSecurity.CreateUserAndAccount "Field does not exist" -
i trying extend simple membership include firstname, secondname , email address, getting problems createuserandaccount.
so in registermodel have added following :-
[display(name = "first name")] [required(errormessage = "text required")] public string firstname { get; set; } [display(name = "second name")] [required(errormessage = "text required")] public string secondname { get; set; } [display(name = "email")] [required(errormessage = "email required")] [regularexpression(@"^\w+([-+.]*[\w-]+)*@(\w+([-.]?\w+)){1,}\.\w{2,4}$")] public string contactemail { get; set; }
in register.cshtml have added user inputs :-
<li> @html.labelfor(m => m.firstname) @html.textboxfor(m => m.firstname) </li> <li> @html.labelfor(m => m.secondname) @html.textboxfor(m => m.secondname) </li> <li> @html.labelfor(m => m.contactemail) @html.textboxfor(m => m.contactemail) </li>
and in accountcontroller.cs have following :-
[httppost] [allowanonymous] [validateantiforgerytoken] public actionresult register(registermodel model) { if (modelstate.isvalid) { // attempt register user try { var userinfo = new { firstname = model.firstname, secondname = model.secondname, contactemail = model.contactemail }; websecurity.createuserandaccount(model.username, model.password, userinfo); websecurity.login(model.username, model.password); return redirecttoaction("index", "home"); } catch (membershipcreateuserexception e) { modelstate.addmodelerror("", errorcodetostring(e.statuscode)); } } // if got far, failed, redisplay form return view(model); }
the problem when tries
websecurity.createuserandaccount(model.username, model.password, userinfo);
i getting error following :-
invalid column name 'firstname'.
invalid column name 'secondname'.
invalid column name 'contactemail'.
is because user table not being created fields? if is, how can create fields. have initialize set
websecurity.initializedatabaseconnection("defaultconnection", "userprofile", "userid", "username", autocreatetables: true);
any appreciated.
thanks
****update*********************
this userprofile
public class userprofile : profilebase { [display(name = "first name")] public virtual string firstname { { return (this.getpropertyvalue("firstname").tostring()); } set { this.setpropertyvalue("firstname", value); } } [display(name = "last name")] public virtual string lastname { { return (this.getpropertyvalue("lastname").tostring()); } set { this.setpropertyvalue("lastname", value); } } [display(name = "contact email")] public virtual string contactemail { { return (this.getpropertyvalue("contactemail").tostring()); } set { this.setpropertyvalue("contactemail", value); } } public static userprofile getprofile(string username) { return create(username) userprofile; } }
you need add these 3 new fields (firstname, secondname , contactemail) userprofile class inherits dbcontext.
userprofile class defined in accounts model.
[table("userprofile")] public class userprofile { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int userid { get; set; } public string username { get; set; } public string firstname { get; set; } public string secondname { get; set; } public string contactemail { get; set; } }
make sure delete tables previous tables deleted.
Comments
Post a Comment