简体   繁体   中英

Using fluent API to set up composite foreign key in one-to-one relation in legacy database

I'm just a beginner in EF code first model. Given two POCO classes mapped to current legacy MS SQL database. They are associated with a composite foreign key setting up one to many relation. Since it's actually one-to-one relation I'd like to have corresponding navigation properties in my POCO objects and do mapping in fluent API. Here is my example:

public partial class Answer
{
    //primary key
    public int id { get; set; }

    //foreign keys
    public int question { get; set; }
    public int assignedForm { get; set; }

    //simple fields
    public short state { get; set; }
    public int author { get; set; }

    //navigation property
    public virtual AssignedQuestion AssignedQuestion { get; set; }
}

 public partial class AssignedQuestion
{
    // primary keys
    public int id { get; set; }
    public int assignedForm { get; set; }

    //simple field
    public string content { get; set; }

    //navigation property
    //public virtual ICollection<Answer> Answers { get; set; }
    public virtual Answer Answer { get; set; }
}

If I wanted to do one-to-many relation I would simply uncomment "Answers" collection and have Fluent API mapping:

modelBuilder.Entity<AssignedQuestion>()
    .HasKey(q => new { q.id, q.assignedForm });

modelBuilder.Entity<Answer>()
    .HasRequired(a => a.AssignedQuestion)
    .WithMany(aq=>aq.Answers)
    .HasForeignKey(a => new { a.question,a.assignedForm});

My goal is to go with one-to-one relation and use "Answer" property in AssignedQuestion with such Fluent API as:

modelBuilder.Entity<AssignedQuestion>()
    .HasKey(q => new { q.id, q.assignedForm });

modelBuilder.Entity<Answer>()
    .HasRequired(a => a.AssignedQuestion)
    .WithOptional(aq => aq.Answer);
    //.HasForeignKey(a => new { a.question, a.assignedForm });  

The problem is I can't specify exactly foreign key fields (as in previous example) and uncomment HasForeignKey call. In this case EF tries to join tables using conventional field names "AssignedQuestion_ID" and "AssignedQuestion_AssignedForm" instead of "question" and "assignedForm" in Answer table. Is there a walkaround in Fluent API other than changing field names?

It is not one-to-one relationship so your first mapping is correct. The reason why it is one-to-many is that EF understands one-to-one only when build on PKs on both sides. If AssignedQuestion has PK id and assignedForm your Answer will need to have FK and PK on its id and assignedForm otherwise EF doesn't see it as one-to-one relation. Even if you mark your question and assignedForm with unique constaint in database (to make it one-to-one in the database) EF will still not be able to handle it as one-to-one because it doesn't support unique constraints yet (except PK).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM