Reputation: 606
Ok I want to know if there is a way to map foreign key relationships with EF Code First Data Annotations instead of using the Fluent API when the names don't match. Here is my scenario. I have two tables with a one-many relationship with a foreign key.
A Security table with: int SecurityID (PK) //Other columns int IncomeFrequencyID (FK)
A Frequency table with int FrequencyID (PK) //Other columns
So IncomeFrequencyID maps to FrequencyID
Then in my Code First:
public class Security
{
public int SecurityID {get;set;}
//Other properties
public int IncomeFrequencyID {get;set;}
//Navigation Properties
public virtual Frequency IncomeFrequency {get;set;}
}
public class Frequency
{
public int FreqencyID {get;set;}
//Other properties
}
Is there a way to accomplish the EF relationship using Data Annotations? Maybe using the AssociationAttribute?
[Association("FK_Security_Frequency", "IncomeFrequencyID", "FrequencyID", IsForeignKey = true)]
Upvotes: 0
Views: 891
Reputation: 119186
Use the ForeignKey
attribute, for example:
public class Security
{
public int SecurityID {get;set;}
//Other properties
public int IncomeFrequencyID {get;set;}
//Navigation Properties
[ForeignKey("IncomeFrequencyID")]
public virtual Frequency IncomeFrequency {get;set;}
}
And on the other side, mark it as a primary key:
public class Frequency
{
[Key]
public int FreqencyID {get;set;}
//Other properties
}
Upvotes: 2