CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    May 2010
    Posts
    7

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured