具有外键的表在edmx中生成的类中缺少成员

本文关键字:成员 edmx | 更新日期: 2023-09-27 18:28:23

我在数据库中有一个表,它有4个外键引用它。当我将表添加到edmx时,表和导航属性就在那里。但是,该表中的外键ID丢失,并且只有虚拟对象存在。

这是在.tt文件中生成的下表:

public partial class Device
{
    public int SolutionId { get; set; }
    public string SiteId { get; set; }
    public string Name { get; set; }
    public int SysId { get; set; }
    public Nullable<int> SysType { get; set; }
    public string SerialNumber { get; set; }
    public Nullable<int> ParentId { get; set; }
    public virtual DeviceModel DeviceModel { get; set; }
    public virtual DeviceType DeviceType { get; set; }
    public virtual SolutionApplication SolutionApplication { get; set; }
    public virtual SolutionType SolutionType { get; set; }
}

缺少几个成员:

DeviceModelId, DeviceTypeId, SolutionApplicationId, and SolutionTypeId

为什么它不见了?有没有办法让这些键真正成为分部类的一部分

使用EntityFrameworks v6.0.2。延迟加载

具有外键的表在edmx中生成的类中缺少成员

简而言之,实体框架"抽象掉了它"。

它足够聪明,可以识别出你的FK代表关系,因此可以让你处理对象本身。因此,您只需要向Device对象添加一个SolutionType对象,并让Entity Framework对其进行排序,而不必担心检查例如SolutionTypeId的FK约束等。(当然,如果您尝试添加违反SolutionType PK的新SolutionType,这会导致问题,因此可能需要首先从SolutionTypes表中查找现有对象)。

因此,与其将其视为通过FK链接到SolutionType表的Device表,不如将其视为由SolutionType对象作为属性的Device对象。EF在保存更改时为您整理数据库(假设您的模型是准确的!)