使用实体框架时,LINQ中的错误(列不存在) [英] Error in LINQ (column doesn't exist) when using Entity Framework

查看:125
本文介绍了使用实体框架时,LINQ中的错误(列不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是我上一个问题的概括:

This question is generalization of my previous question:

在实体框架中使用PostGreSQL时,LINQ中出错
$
我有假设,如果在2之间表格存在1个以上的关系(7为我的示例),EF尝试通过添加附加列来对此表进行规范化。

Error in LINQ when work with PostGreSQL by Entity Framework
I have hypothesis, that, If between 2 tables exist more than 1 relation (7 for my example), EF try to normalize this table by adding additional column.

例如,模型a:

public partial class a
{        
    [Key]
    public int id { get; set; }

    [ForeignKey("contractors"), Column(Order = 0)]
    public Nullable<int> ot_contractor_id { get; set; }

    [ForeignKey("contractors1"), Column(Order = 1)]
    public Nullable<int> gvo_contractor_id { get; set; }

    public virtual contractors contractors { get; set; }
    public virtual contractors contractors1 { get; set; }    
}

表[a]与表[承包商]有关系[id]所以,EF生成列的contractors_id和contractors1_id。

Table [a] have relations to table [contractors].[id] So, EF generate column's "contractors_id" and "contractors1_id".

其他表只有1个关系,它们正常工作。

Other tables have only 1 relation and they work normal!

问题:这个假设是否正确?多余列的问题来自于与几个关系的异常表?
谢谢!

The question: Is that hypothesis correct?! And the problem of excess column comes from unnormal tables with a few relations? Thanks!

推荐答案

如果您在承包商中有逆向导航属性, code> class,like so:

It can happen if you have inverse navigation properties in contractors class, like so:

public partial class contractors
{
    //...

    public virtual ICollection<a> aCollection { get; set; }
    public virtual ICollection<a> aCollection1 { get; set; }
}

在这种情况下,EF不会知道哪一个属于哪个导航属性在类 a 中,假设四个关系而不是两个关系(如果只有一个集合,则假定为三个而不是两个关系)。这些附加关系将有一个单独的外键,其中一个是 contractors_id 。如果数据库中不存在,您将收到一个例外。

In this case EF won't know which one belongs to which navigation property in class a and assume four relationships instead of two (or three instead of two if you have only one collection). Those additional relationships will have a separate foreign key and one of them is contractors_id. If that doesn't exist in the database you get an exception.

您可以通过应用 InverseProperty 属性在 a

You can fix the problem by applying the InverseProperty attribute in class a:

[InverseProperty("aCollection")]
public virtual contractors contractors { get; set; }
[InverseProperty("aCollection1")]
public virtual contractors contractors1 { get; set; }

这篇关于使用实体框架时,LINQ中的错误(列不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆