简体   繁体   中英

Adding a second one-to-one relationship with table Entity Framework 6 fluent api

I've been task with creating a web app using code-first, I've added a first one-to-one relationship which works as I expected, I then added a second one-to-one relationship but can't get it to work.

Here are my models (most properties removed for brevity)...

FollowUpAssessment.cs

public class FollowUpAssessment
{
    [Key]
    public int FollowUpAssessmentId { get; set; }
    public virtual FullBloodCount FullBloodCount { get; set; }
    public virtual AdverseEvent AdverseEvent { get; set; }
}

FullBloodCount.cs

public class FullBloodCount
{
    [Key]
    [ForeignKey("FollowUpAssessment")]
    public int FollowUpAssessmentId { get; set; }

    public virtual FollowUpAssessment FollowUpAssessment { get; set; }
}

AdverseEvent.cs

public class AdverseEvent
{
    [Key]
    [ForeignKey("FollowUpAssessment")]
    public int FollowUpAssessmentId { get; set; }

    public virtual FollowUpAssessment FollowUpAssessment { get; set; }
}

Context

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<FollowUpAssessment>()
                    .HasOptional(s => s.FullBloodCount)
                    .WithRequired(s => s.FollowUpAssessment);

        modelBuilder.Entity<FollowUpAssessment>()
                    .HasOptional(s => s.AdverseEvent)
                    .WithRequired(s => s.FollowUpAssessment);
}

When adding a follow up the full blood count data is added to its table correctly with the foreign key, but try as I may I cannot seem to make the adverse event data populate the table.

I've spent most of today looking at this, and have search the internet and only seem able to find .NET Core solutions for this. I'm having to work with .NET 4.7.1, MVC 5 and EF 6.2.0 to complete this task. I would be grateful for any suggestions to make it work as needed thanks!

我已经解决了这个问题,它与我当时正在处理的流畅的 API 代码无关 - 它与 DateTime 问题有关。

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