我有一个数据库优先的实体框架核心项目,我有以下模型:
public partial class Message
{
[Key]
[Column("Message_ID")]
public int MessageId { get; set; }
[Column("Debate_ID")]
public int DebateId { get; set; }
[Column("Message")]
public string Content { get; set; } = null!;
[Column("AnwersToMessageId")]
public int? AnwersToMessageId { get; set; }
[InverseProperty("AnwersToMessage")]
public virtual ICollection<Message> InverseAnwersToMessage { get; } = new List<Message>();
[ForeignKey("AnwersToMessageId")]
[InverseProperty("InverseAnwersToMessage")]
public virtual Message? AnwersToMessage { get; set; }
[ForeignKey("DebateId")]
[InverseProperty("Messages")]
public virtual Debate Debate { get; set; } = null!;
}
public partial class Debate
{
[Key]
[Column("Debate_ID")]
public int DebateId { get; set; }
[Column("Title")]
public string Title { get; set; } = null!;
[InverseProperty("Debate")]
public virtual ICollection<Message> Messages { get; } = new List<Message>();
}
我在上下文中还有以下内容:
modelBuilder.Entity<Message>(entity =>
{
entity.HasOne(d => d.AnwersToMessage).WithMany(p => p.InverseAnwersToMessage).HasConstraintName("FK_Message_Message");
entity.HasOne(d => d.Debate).WithMany(p => p.Messages).HasConstraintName("FK_Message_Debate");
});
然后我设置了搜索存储库和功能以在数据库中搜索这些内容。随着查询
var debate = _dbSet.OfType<Debate>().Where(d => d.DebateId == saetchId).FirstOrDefault();
我能够找到有问题的辩论,但它留下了所有相关的信息。所以我修改成这样:
var debate = _dbSet.OfType<Debate>().Include(m => m.Messages).Where(d => d.DebateId == searchId).FirstOrDefault();
这会产生错误:
Microsoft.Data.SqlClient.SqlException: '无效的列名'MessageId1'。'
我试过没有成功地找到这个问题的答案。我试图将导航属性添加到 Message 和 Debate 中,但同样的错误不断发生,我不知所措......