【问题标题】:EF Core Invalid Column Name (Foreign Keys)EF Core 列名无效(外键)
【发布时间】:2019-03-27 07:45:50
【问题描述】:

我使用的是 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- 父客户案例

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

详细实体Case_Session

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 为外键创建自己的命名约定的错误 但我认为标签外键覆盖了该命名约定

现在我哪里错了?

【问题讨论】:

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


    【解决方案1】:

    [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 表中的主键类型很长?

    【讨论】:

    • 您的意思是 [ForeignKey("Case_Status")] 取列的名称而不是表的名称??但我将表的名称放在数据注释中,它正在其他地方工作
    • 是的,列的名称,但它们也可以取实体名称。
    • 现在我很困惑,一开始你在写列名,然后你在写表名
    • 两者都有效,你做的方式有点不对。您使用类似:[ForeignKey("Clients")],它应该是客户端而不是客户端。所以正确的是:[ForeignKey("Client")]
    猜你喜欢
    • 2021-08-19
    • 1970-01-01
    • 2021-04-29
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多