简体   繁体   English

添加与表 Entity Framework 6 fluent api 的第二个一对一关系

[英]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 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全血计数.cs

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

    public virtual FollowUpAssessment FollowUpAssessment { get; set; }
}

AdverseEvent.cs不良事件.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.我今天大部分时间都在看这个,并在互联网上搜索,似乎只能找到 .NET Core 解决方案。 I'm having to work with .NET 4.7.1, MVC 5 and EF 6.2.0 to complete this task.我必须使用 .NET 4.7.1、MVC 5 和 EF 6.2.0 来完成这项任务。 I would be grateful for any suggestions to make it work as needed thanks!我将不胜感激任何建议,使其根据需要工作谢谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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