|
-
September 18th, 2011, 03:44 AM
#1
Code First Programming
Hello All,
I Have this code:
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; }
}
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:
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();
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.
Thanx in advance for the Help.
Last edited by VitalyUr; September 18th, 2011 at 03:53 AM.
-
September 18th, 2011, 06:44 AM
#2
Re: Code First Programming
One common way would be to have dropdown lists or combos on your form that will contain the list of job titles and departments from those tables so the user can select one
Always use [code][/code] tags when posting code.
-
September 19th, 2011, 01:42 AM
#3
Re: Code First Programming
Hi,
As DataMiser said associate any list or dropdown box to SQL Table so that user can select from it .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|