I'm having trouble with reverse navigation on one of my entities.
I have the following two objects:
public class Candidate
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long CandidateId { get; set; }
....
// Reverse navigation
public virtual CandidateData Data { get; set; }
...
// Foreign keys
....
}
public class CandidateData
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long CandidateDataId { get; set; }
[Required]
public long CandidateId { get; set; }
// Foreign keys
[ForeignKey("CandidateId")]
public virtual Candidate Candidate { get; set; }
}
Now my foreign key navigation on the CandidateData object works fine. I am having trouble getting the reverse navigation for the candidate object to work (if that's even possible).
This is my OnModelCreating function:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Candidate>()
.HasOptional(obj => obj.Data)
.WithOptionalPrincipal();
base.OnModelCreating(modelBuilder);
}
It's close to working except in the database I get two columns that link to the CandidateId. I get the one I from the POCO object the I get another column Candidate_CandidateId I assume was created by the modelBuilder.
I am quiet lost at the moment. Can someone please shed some light on what's going on?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…