EF核心无效列名称(外键)

时间:2019-03-27 07:45:51

标签: c# .net-core core ef-core-2.2

我正在使用EF Core 2.2 错误代码

var ClientCase= _context.Client_Cases.Include(a=>a.Case_Sessions). FirstOrDefault(x => x.Id == id);

错误

  

System.Data.SqlClient.SqlException:'无效的列名   “ Client_CaseId”。无效的列名“ Case_LevelId”。无效的栏   名称“ Client_CaseId”。无效的列名“ Court_CircleId”。无效   列名称“ Court_HallId”。

实体

1-父Client_Case

public class Client_Cases
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }        
    public string Opponent { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime Recieve_Date { get; set; }
    [ForeignKey("Clients")]
    public long? ClientID { get;set;}
    public Clients Client { get; set; }
    [ForeignKey("Case_Levels")]
    public long? LevelID { get; set; }
    public virtual Case_Levels Case_Levels { get; set; }
    [ForeignKey("Case_Types")]
    public long? TypeID { get; set; }
    public virtual Case_Types Case_Types { get; set; }
    [ForeignKey("Court_Circles")]
    public long? CircleID { get; set; }
    public virtual Court_Circles Court_Circles { get; set; }
    [ForeignKey("Court_Halls")]
    public long? HallID { get; set; }
    public virtual Court_Halls Court_Halls { get; set; }
    [ForeignKey("Courts")]
    public long? CourtID { get; set; }
    public virtual Courts Court { get; set; }
    [ForeignKey("Case_Status")]
    public long? StatusID { get; set; }
    public Case_Status Case_Status { get; set; }
    [ForeignKey("Lawyers")]
    public long? LawyerID { get; set; }
    public virtual LawyersData Lawyers { get; set; }
    public string Description { get; set; }
    public string Code { get; set; }
    public string CaseNo { get; set; }
    public List<Case_Sessions> Case_Sessions { get; set; }
}

详细实体案例会话

public class Case_Sessions
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }        
    [ForeignKey("Client_Cases")]
    public long? CaseID { get;set;}
    public Client_Cases Client_Case { get; set; }
    [ForeignKey("Case_Levels")]
    public long? LevelID { get; set; }
    public Case_Levels Case_Level { get; set; }
    [ForeignKey("Court_Circles")]
    public long? CircleID { get; set; }
    public Court_Circles Court_Circle { get; set; }
    [ForeignKey("Court_Halls")]
    public long? HallID { get; set; }
    public Court_Halls Court_Hall { get; set; }
    [ForeignKey("Case_Status")]
    public long? StatusID { get; set; }
    public Case_Status Case_Status { get; set; }
    public DateTime Session_Date { get; set; }
    public string Judge_Name { get; set; }
    public string Session_Result { get; set; }
    public string Notes { get; set; }
}

如果我的父母没有包括孩子,那它会起作用 如果我知道它的细节

我知道EF Core为外键创建自己的命名约定的错误 但我认为外键标签会覆盖该命名约定

现在我错了吗?

谢谢

1 个答案:

答案 0 :(得分:2)

[ForeignKey(“”)]是什么意思?命名您在课堂上添加的属性以成为外键。例如:

public long? CaseID { get;set;}
[ForeignKey("CaseID")]
public Client_Cases Client_Case { get; set; }

public long? CircleID { get; set; }
[ForeignKey("CircleID")]
public Court_Circles Court_Circle { get; set; }

您可以使用上面的注释,在您的情况下,需要以下更正:

[ForeignKey("Client")] // it should be [ForeignKey("Client")] not an extra s if you using entities name in annotation.
    public long? ClientID { get;set;}
    public Clients Client { get; set; }

这应该是您与律师的关系

[ForeignKey("Lawyers")]
    public long? LawyersID { get; set; }
    public virtual LawyersData Lawyers { get; set; }

我假设LawyersData表中的主键类型很长?