LINQPad 使用什么信息来生成关联 attributes/Navigation 属性?

What information does LINQPad use to generate Association attributes/Navigation Properties?

我正在使用 LINQPad 连接到 SQL 服务器数据库,然后生成 TypedDataContext。

在旧版本的数据库中,tables(不是通过 ASP.NET C# 代码生成的)具有其他 tables 的导航属性。

在一个团队开发的新版本中,没有导航属性。

我的第一直觉是缺少约束,事实证明这是真的。开发人员将外键约束添加到 table 后,仍然没有导航属性。

使用ILSpy for LINQPad,我对table中表示的类型进行了反思,发现在一个对象中存在具有Association属性的属性:

[XmlIgnore]
[Association(Name = "FK_AvailabilityActivity_SFT_WK_Shift", Storage = "_Ds_Shift", ThisKey = "SFT_WK,SFT_WK", OtherKey = "SFT_WK,SFT_WK", IsForeignKey = true)]
public Ds_Shift Ds_Shift
{
    get
    {
        //Error decoding local variables: Signature type sequence must have at least one element.
        return _Ds_Shift.Entity;
    }
    set
    {
        //Error decoding local variables: Signature type sequence must have at least one element.
        _Ds_Shift.Entity = value;
    }
}

而另一个新对象则没有。

那么,我的问题是:

LINQPad 使用什么信息来生成这些属性?

一旦我知道这一点,我应该能够使用导航属性添加该信息并跨 table 查询。

根据 Joe Albahari(LINQPad 的作者)的说法:

It just reads the foreign key constraints and builds it from that. Sometimes it's unable to infer a name for an association, or the name is duplicated in another association or property, but that's quite rare.

所以我对缺少约束的怀疑是正确的,碰巧当开发人员添加缺少的约束时,table 的主键被遗漏了(由于另一列有一个聚集索引,SQL 服务器在您分配主键时默认尝试创建,我们使用 NONCLUSTERED 关键字解决了这个问题)。