Hello All,
I Have this code:
When i create the database the fields jobTitle and department in Users are created as Foreign Keys in the Database, i've inserted some values into the Departments and jobTitles tables manually and now i am trying to create a new user like so:Code:public class Users { [DatabaseGenerated(DatabaseGeneratedOption.None)] public int ID { get; set; } public string Password { get; set; } public string firstName { get; set; } public string lastName { get; set; } public virtual JobTitles jobTitle { get; set; } public virtual Departments department { get; set; } public DateTime birthDate { get; set; } public string phoneNumber { get; set; } public string status { get; set; } public string email { get; set; } public byte[] Picture { get; set; } } public class Departments { public int ID { get; set; } public string Department { get; set; } } public class JobTitles { public int ID { get; set; } public string jobTitle { get; set; } public Departments Department { get; set; } } public class WorkNetDAL : DbContext { public WorkNetDAL() : base("WorkNet") { } public DbSet<Users> Users { get; set; } public DbSet<Departments> Departments { get; set; } public DbSet<JobTitles> jobTitles { get; set; } }
My Question is, how do i assign a department and jobtitle to the user? i want to use the already existing department and jobtitle from the DB.Code:WorkNetDAL db = (WorkNetDAL)Session["db"]; var user = new Users { birthDate = DateTime.Parse(txtDatePicker.Text), email = txtEmail.Text.ToString(), firstName = txtFN.Text, lastName = txtLN.Text, Password = txtPass.Text, phoneNumber = ddlAreaCode.SelectedValue + "-" + txtPhone.Text, Picture = fuPic.FileBytes, ID = int.Parse(txtID.Text) }; db.Users.Add(user); db.SaveChanges();
Thanx in advance for the Help.




Reply With Quote