将多对一关系转换为一对一EF [英] Change many-to-one relationship into one-to-one EF

查看:62
本文介绍了将多对一关系转换为一对一EF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.NET CORE 2.1构建平台,它是关于问题和答案的平台.在开始之前,我将现有的数据库脚手架放入.net项目中,因此我启动了模型并开始运行.

I am building a platform using .NET CORE 2.1, and it is about questions and answers. Before I started I scaffolded my existing DB into the .net project so I got the model up and running.

Scaffold-DbContext服务器=(localdb)\ mssqllocaldb;数据库= MYDATABASE; Trusted_Connection = True;"Microsoft.EntityFrameworkCore.SqlServer -OutputDir模型

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=MYDATABASE;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

但是它生成了具有多对一关系的QUESTION和QUESTION_DETAIL,而QUESTION应该只包含1个QUESTION DETAIl.但是,当我尝试在上下文中使用流畅的API进行更改时,会出现错误:

But it generated QUESTION and QUESTION_DETAIL with a many-to-one relation ship whereas QUESTION should only have 1 QUESTION DETAIl. But when I am trying to change it with the fluent API in my context it gives an error:

无法将lambda表达式转换为类型类型",因为它不是委托类型

Cannot Convert lambda expression to type 'Type' because it is not a delegated type

这是我的2个模型课程

public partial class Question
{
    public Question()
    {
        AnswerClientBot = new HashSet<AnswerClientBot>();
        InverseOriginalQuestion = new HashSet<Question>();
        QuestionFollowUpFollowUpQuestion = new HashSet<QuestionFollowUp>();
        QuestionFollowUpOriginalQuestion = new HashSet<QuestionFollowUp>();
    }

    public int Id { get; set; }
    public int BotInstanceId { get; set; }
    public string LanguageCode { get; set; }
    public string Question1 { get; set; }
    public int? OriginalQuestionId { get; set; }
    [ForeignKey("QuestionDetail")]
    public int QuestionDetailId { get; set; }

    public Instance BotInstance { get; set; }
    public Question OriginalQuestion { get; set; }
    public QuestionDetail QuestionDetail { get; set; }
    public ICollection<AnswerClientBot> AnswerClientBot { get; set; }
    public ICollection<Question> InverseOriginalQuestion { get; set; }
    public ICollection<QuestionFollowUp> QuestionFollowUpFollowUpQuestion { get; set; }
    public ICollection<QuestionFollowUp> QuestionFollowUpOriginalQuestion { get; set; }
}

public partial class QuestionDetail
{
    public int Id { get; set; }
    public int OriginalQuestionId { get; set; }
    public string Topic { get; set; }
    public string Intent { get; set; }
    public string CustDetail01 { get; set; }
    public string CustDetail02 { get; set; }
    public string CustDetail03 { get; set; }
    public string CustDetail04 { get; set; }
    public string CustDetail05 { get; set; }
    public string Keywords { get; set; }

    public Question OriginalQuestion { get; set; }
}

这是我要更改的上下文,错误发生在

And this is the context I am trying to change, the error occurs on

HasForeignKey(d => d.OriginalQuestionId)

modelBuilder.Entity<QuestionDetail>(entity =>
        {
            entity.ToTable("QuestionDetail", "Bot");

            entity.Property(e => e.CustDetail01)
                .HasColumnName("CUST_Detail01")
                .HasMaxLength(250)
                .IsUnicode(false);

            entity.Property(e => e.CustDetail02)
                .HasColumnName("CUST_Detail02")
                .HasMaxLength(250)
                .IsUnicode(false);

            entity.Property(e => e.CustDetail03)
                .HasColumnName("CUST_Detail03")
                .HasMaxLength(250)
                .IsUnicode(false);

            entity.Property(e => e.CustDetail04)
                .HasColumnName("CUST_Detail04")
                .HasMaxLength(250)
                .IsUnicode(false);

            entity.Property(e => e.CustDetail05)
                .HasColumnName("CUST_Detail05")
                .HasMaxLength(250)
                .IsUnicode(false);

            entity.Property(e => e.Intent)
                .HasMaxLength(250)
                .IsUnicode(false);

            entity.Property(e => e.Keywords)
                .HasMaxLength(250)
                .IsUnicode(false);

            entity.Property(e => e.Topic)
                .HasMaxLength(250)
                .IsUnicode(false);

            entity.HasOne(d => d.OriginalQuestion)
                .WithOne(p => p.QuestionDetail)
                .HasForeignKey(d => d.OriginalQuestionId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Bot_QuestionDetail_OriginalQuestionId");
        });

有人知道我该如何解决吗?谢谢!

Does anyone have any clue how I can fix this? Thank you!

推荐答案

声明两个具有导航属性的类.在其主键上用ForeignKey属性标记一个表(从属表).EF从中推断出一对一:

Declare both classes with navigation properties to each other. Mark one of the tables (the dependent table) with the ForeignKey attribute on its Primary Key. EF infers 1-to-1 from this:

public class Question
{
    ...
    //  [ForeignKey("QuestionDetail")]
    //  public int QuestionDetailId { get; set; }
    public QuestionDetail QuestionDetail { get; set; }
    ...
}
public partial class QuestionDetail
{
   //public int Id { get; set; }
   [ForeignKey("Question")]
   public int QuestionId { get; set; }
   ...
   public Question Question { get; set; }
}

这篇关于将多对一关系转换为一对一EF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆