c# - ASP.NET MVC insert in two separate tables -


hello newbie in asp.net mvc.

i have 3 classes login, user , thetraderscontext can see below:

namespace simpleuser.models {     [table("login")]     public class login     {         [key]         [databasegeneratedattribute(databasegeneratedoption.identity)]         public int id { get; set; }          [required]         [stringlength(50)]         [display(name = "email")]         public string email { get; set; }          [required]         [datatype(datatype.password)]          [display(name = "password")]         public string password { get; set; }          public string status { get; set; }          public string salt { get; set; }     } } namespace simpleuser.models {     [table("userdata")]     public class user     {          public int id { get; set; }         public string surname { get; set; }         public string name { get; set; }         public string sex { get; set; }         public string city { get; set; }         public string address { get; set; }         public string zipcode { get; set; }         public string tel { get; set; }         public string bdate { get; set; }         public string country { get; set; }     } } namespace simpleuser.models {     public class thetraderscontext : dbcontext     {         public dbset<login> loginusers { get; set; }         public dbset<user> allusers { get; set; }      } } 

then created logincontroller.cs has register function in try pass in 2 different tables of database elements take formcollection can see below. problem in database passing in table login , not in userdata.

[httppost] public actionresult registration(formcollection forms){      var db = new thetraderscontext();     var crypto = new simplecrypto.pbkdf2();      var p = forms["password"].tostring();     string encryptpass = crypto.compute(p);       var newuser = db.loginusers.create();      var nuser = db.allusers.create();       newuser.email = forms["email"].tostring();      newuser.password = encryptpass;      newuser.status = "user";      newuser.salt = crypto.salt;       //nuser.id=convert.toint32("18");     nuser.surname = forms["lastname"].tostring();     nuser.name = forms["firstname"].tostring();     nuser.sex = forms["gender"].tostring();     nuser.city = forms["city"].tostring();     nuser.address = forms["addr"].tostring();     nuser.zipcode =forms["zip"].tostring();     nuser.tel = "fdgfdgf".tostring();     nuser.country = forms["country"].tostring();     nuser.bdate ="dsafdsaf".tostring();      try     {        db.loginusers.add(newuser);        db.savechanges();         var useri = db.loginusers.single(u => u.email == newuser.email);         if (useri == null) {             throw new exception();         }           nuser.id = convert.toint32(useri.id);         db.allusers.add(nuser);          db.savechanges();      }     catch (exception x)     {         modelstate.addmodelerror("", "this username in use");     }      return view(); } 

my table in database has same names of fields on user.

of course tried exclude code has login , pass values of userdata in database saw exception : system.data.entity.infrastructure.dbupdateexception.

i have tried lot of things until now... idea?

from code seems there relation 1 many between login , user, each login has 1 or more user ( figured out since trying put nuser.id = convert.toint32(useri.id);)

in class user put navigational property called public login login{get; set;}

and user class should has primary key let ( userid) unless marked id primary key user table weak entity. , mark foreignkey attribute new property login foreignkey("id")

after doing this, can following

var login=new login(); // fill out login data db.loginusers.add(login)  db.allusers.add(new user(){    surname = forms["lastname"].tostring(),    name = forms["firstname"].tostring(),    sex = forms["gender"].tostring(),    city = forms["city"].tostring(),    address = forms["addr"].tostring(),    zipcode =forms["zip"].tostring(),    tel = "fdgfdgf".tostring(),    country = forms["country"].tostring(),    bdate ="dsafdsaf".tostring(),    login=login  });  db.savechanges(); 

hope you

note: classes design can improved , normalized reflect database relationship


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -