【问题标题】:Change many-to-one relationship into one-to-one EF将多对一关系改为一对一 EF
【发布时间】:2018-07-24 14:48:18
【问题描述】:

我正在使用 .NET CORE 2.1 构建一个平台,它是关于问题和答案的。在开始之前,我将现有的数据库搭建到 .net 项目中,以便让模型启动并运行。

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

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

无法将 lambda 表达式转换为类型“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; }
}

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

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");
        });

有人知道我该如何解决这个问题吗?谢谢!

【问题讨论】:

    标签: entity-framework asp.net-core


    【解决方案1】:

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

    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 会按照约定发现它,您也可以省略 ForeignKeyAttribute。例如,名为“Question”的导航属性和名为“QuestionID”的外键属性。或者您可以将[ForeignKey("QuestionID")] 添加到导航属性,甚至省略外键属性。 EF Core 将为外键使用影子属性。
    【解决方案2】:

    所以我找到了这个Cannot convert lambda expression to type 'object' because it is not a delegate type stackoverflow 帖子,一开始我尝试强输入它,但不知道怎么做,但最终我想通了。所以我把它改成了HasForeignKey&lt;Question&gt;(d =&gt; d.OriginalQuestionId),它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多