texas697
texas697

Reputation: 6397

How to use Fluent API to map foreign key

I am trying to use modelBuilder to map a foreign Key in my database. I need to post the Id into JobTESPM_EmployeeId instead of JOBTESPMId. I have been using this a guide but I can't see where the examples are the same as my setup. http://msdn.microsoft.com/en-in/data/jj591620.aspx#IndependentAssociation

 public class Job
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
    public Int64? JobId { get; set; }
    public int? JobNumber { get; set; }
    public string JobName { get; set; }
    public int? JobTypeId { get; set; }

    public int? CustomerEmployeePMId { get; set; }
    public virtual CustomerEmployee CustomerEmployeePM { get; set; }

    public int? CustomerEmployeeAdminId { get; set; }
    public virtual CustomerEmployee CustomerEmployeeAdmin { get; set; }

    public int? CustomerEmployeeAccountantId { get; set; }
    public virtual CustomerEmployee CustomerEmployeeAccountant { get; set; }

    public int? CustomerEmployeeSuperintendentId { get; set; }
    public virtual CustomerEmployee CustomerEmployeeSuperintendent { get; set; }

    public int? JobTESPMId { get; set; }
    public virtual Employee JobTESPM { get; set; }

    public int? JobTESSuperintendentId { get; set; }
    public virtual Employee JobTESSuperintendent { get; set; }
}

public class Employee
 {
    public int EmployeeId { get; set; }
    public string AccountName { get; set; }
    public string EmployeeFirstName { get; set; }
    public string EmployeeLastName { get; set; }
    public string EmployeeTitle { get; set; }
    public Int64? EmployeeCellPhone { get; set; }
    public Int64? EmployeeOfficePhone { get; set; }
    public string EmployeeEmail { get; set; }
    public int? CompanyEmployeeId { get; set; }
    public bool? EmployeeHidden { get; set; }
    public bool? EmployeeIsSuper { get; set; }
    public bool? EmployeeIsPM { get; set; }
    public string EmployeeAltEmail { get; set; }
 }

pic

Upvotes: 0

Views: 87

Answers (1)

Kelly
Kelly

Reputation: 995

What about adding a couple attributes...

[Column("JobTESPM_EmployeeID")]
public int? JobTESPMId { get; set; }
[ForeignKey("JobTESPMId")]
public virtual Employee JobTESPM { get; set; }

Upvotes: 1

Related Questions