2

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 "Server=(localdb)\mssqllocaldb;Database=MYDATABASE;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

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:

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

Here are my 2 model classes

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!

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320

2 Answers2

2

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; }
}
Hossein
  • 3,083
  • 3
  • 16
  • 33
  • 2
    You can also omit the ForeignKeyAttribute if EF will discover it by convention. Eg Navigation Property named "Question" and Foreign Key Property named "QuestionID". Or you can add `[ForeignKey("QuestionID")]` to the Navigation Property and even omit the Foreign Key property. EF Core will use a Shadow Property for the Foreign Key. – David Browne - Microsoft Jul 23 '18 at 14:53
1

So I found this Cannot convert lambda expression to type 'object' because it is not a delegate type stackoverflow post, and at the start I tried to strong type it but didn't know really how to but eventually I figured it out. So I changed it to HasForeignKey<Question>(d => d.OriginalQuestionId) which worked for me.