Robert Ross
Robert Ross

Reputation: 1189

How to update the foreign key in a one-to-many relationship mapped with EF?

I created a one-to-many relationship using the EF convention for that. Then after the update-database I got a Company_CompanyId column in the WorkRoles table, which was the desired result.

But when I tried to actually use this foreign key, so I can connect the different roles with different companies I got stuck.

In my WorkRoleController I expected to see be able to access the FK property in the way I can access the other properties, but this wasn't the case.

When I type workrole(dot) I see the RoleName, RoleDescription, WorkRoleId and, instead of Company_CompanyId, I see just Company. Which is the object Company, not the foreign key.

So my question is, how can I use that foreign key, so when creating a new role I can do something like :

Company company = db.Companies.Where(c => c.CAId == currentUserId)
                              .FirstOrDefault();

int companyID = company.CompanyId;    // find the company for the current user

workRole.Company_CompanyId = companyID;    // save the company ID to the FK field.

Can someone help with that? How can I actually save a foreign key when saving a workrole?

Role model :

public class WorkRole
{
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int WorkRoleId { get; set; }
        public string RoleName { get; set; }
        public string RoleDescription { get; set; }
        public virtual Company Company { get; set; }
}

Company model :

public class Company
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CompanyId { get; set; }
    public byte[] ImageData { get; set; }
    [NotMapped]
    public HttpPostedFileBase UploadImage { get; set; }
    [NotMapped]
    public string ImageBase64 => System.Convert.ToBase64String(ImageData);
    public string CompanyName { get; set; }
    public string CAId { get; set; }
    public virtual ICollection<WorkRole> WorkRoles { get; set; }
}

Upvotes: 0

Views: 189

Answers (1)

Gimballock
Gimballock

Reputation: 477

Add CompanyIdproperty to your WorkRole class, push changes to the database and then you should see it when you type workrole(dot) and update it as you like.

public class WorkRole
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int WorkRoleId { get; set; }
        public string RoleName { get; set; }
        public string RoleDescription { get; set; }
        public int CompanyId {get; set;}// this will be mapped as foreign key by EF convention.
        public virtual Company Company { get; set; }
    }

If your foreign key property name doesn't follow any of the EF conventions, you can use [ForeignKey] data annotation to let EF know that the property you just defined is actually the foreign key:

public class WorkRole
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int WorkRoleId { get; set; }
        public string RoleName { get; set; }
        public string RoleDescription { get; set; }
        [ForeignKey("Company")]
        public int Foo {get; set;}
        public virtual Company Company { get; set; }
    }

You can also use it on the navigation property:

public class WorkRole
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int WorkRoleId { get; set; }
        public string RoleName { get; set; }
        public string RoleDescription { get; set; }
        public int Foo {get; set;}
        [ForeignKey("Foo")]
        public virtual Company Company { get; set; }
    }

Upvotes: 2

Related Questions