c# - adding company model to account class -
i trying add model within account model class so
public class registerviewmodel { [required] [emailaddress] [display(name = "email")] public string email { get; set; } [required] [stringlength(100, errormessage = "the {0} must @ least {2} characters long.", minimumlength = 6)] [datatype(datatype.password)] [display(name = "password")] public string password { get; set; } [datatype(datatype.password)] [display(name = "confirm password")] [compare("password", errormessage = "the password , confirmation password not match.")] public string confirmpassword { get; set; } public int companyid { get; set; } public virtual companydetails company { get; set; } } public class companydetails { [key] public int companyid { get; set; } [stringlength(100, errormessage = "the {0} must @ least {2} characters long.", minimumlength = 1)] [display(name = "company name")] public string companyname { get; set; } } what not sure how create dbset company class , id column of company show in users table?
mvc 5 utilizes identity, among other things comes default applicationuser class. application's "user" , persisted entity framework database. result, need add additional relationships here, not registerviewmodel, it's name indicates, view model, not entity.
identitymodels.cs
public class applicationuser : identityuser { public async task<claimsidentity> generateuseridentityasync(usermanager<applicationuser> manager) { // note authenticationtype must match 1 defined in cookieauthenticationoptions.authenticationtype var useridentity = await manager.createidentityasync(this, defaultauthenticationtypes.applicationcookie); // add custom user claims here return useridentity; } public virtual companydetails company { get; set; } } once generate migration , update database, dbo.companydetails table created , foreign key table added dbo.aspnetusers (the table applicationuser)
you'll of course need keep property on registerviewmodel in order edit fields view model, can remove virtual keyword. virtual keyword means property or method can overridden, necessary in case of navigation property on entity entity framework can attach lazy-loading logic on proxy classes creates. that's more info need, long , short, it's not needed on view model.
Comments
Post a Comment