我正在使用.NET CORE 2.1构建平台,它是关于问题和答案的平台。在开始之前,我将现有的数据库脚手架放入.net项目中,因此我启动了模型并开始运行。
Scaffold-DbContext“服务器=(localdb)\ mssqllocaldb;数据库= MYDATABASE; Trusted_Connection = True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir模型
但是它生成了具有多对一关系的QUESTION和QUESTION_DETAIL,而QUESTION应该只有1个QUESTION DETAIl。但是,当我尝试在上下文中使用流畅的API进行更改时,会出现错误:
无法将lambda表达式转换为类型“类型”,因为它不是委托类型
这是我的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");
});
有人知道我该如何解决吗?谢谢!
答案 0 :(得分:2)
相互声明两个具有导航属性的类。在其主键上用ForeignKey属性标记一个表(从属表)。 EF从中推断出一对一:
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; }
}
答案 1 :(得分:0)
因此,我找到了这个Cannot convert lambda expression to type 'object' because it is not a delegate type stackoverflow帖子,一开始我试图对其进行强类型化,但实际上却不知道该怎么做,但最终我发现了。因此,我将其更改为HasForeignKey<Question>(d => d.OriginalQuestionId)
对我有用。