Entity Framework 不具有约束力的实体

Entity Framework not binding entity

我有以下数据库结构:

我正在使用 EF6 从数据库创建实体,并具有由 EF6 创建的以下 类:

  public partial class Mechanic
   {
    public Mechanic()
    {
        this.MechanicAddresses = new HashSet<MechanicAddress>();
        this.MechanicServices = new HashSet<MechanicService>();
    }

    public string ID { get; set; }
    public string Email { get; set; }
    public bool EmailConfirmed { get; set; }
    public string PasswordHash { get; set; }
    public string SecurityStamp { get; set; }
    public string PhoneNumber { get; set; }
    public bool PhoneNumberConfirmed { get; set; }
    public bool TwoFactorEnabled { get; set; }
    public Nullable<System.DateTime> LockoutEndDateUtc { get; set; }
    public bool LockoutEnabled { get; set; }
    public int AccessFailedCount { get; set; }
    public string UserName { get; set; }
    public string CompanyName { get; set; }
    public string ContactName { get; set; }
    public string MobileNumber { get; set; }
    public Nullable<bool> IsMobile { get; set; }
    public string Url { get; set; }
    public string FaceBookUrl { get; set; }
    public string TwitterUrl { get; set; }
    public string Description { get; set; }
    public string Discriminator { get; set; }
    public bool IsEnabled { get; set; }
    public bool IsAuthorised { get; set; }
    public string Logo { get; set; }
    public System.DateTime CreationTimestamp { get; set; }

    public virtual ICollection<MechanicAddress> MechanicAddresses { get; set; }
    public virtual ICollection<MechanicService> MechanicServices { get; set; }
    }


public partial class MechanicAddress
{
    public int ID { get; set; }
    public string MechanicId { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public string District { get; set; }
    public string Region { get; set; }
    public string PostalCode { get; set; }
    public int CountryId { get; set; }
    public System.DateTime CreationTimestamp { get; set; }
    public bool IsPrimary { get; set; }
    public Nullable<double> Latitude { get; set; }
    public Nullable<double> Longitude { get; set; }
    public System.Data.Entity.Spatial.DbGeography Location { get; set; }

    public virtual Country Country { get; set; }
    public virtual Mechanic Mechanic { get; set; }
}


public partial class MechanicService
{
    public int ID { get; set; }
    public string MechanicId { get; set; }
    public string Service { get; set; }

    public virtual Mechanic Mechanic { get; set; }
}

数据正确,因此我希望获得所有实体中的数据。

当我 运行 在我的 DAL 中执行以下 linq 查询时:

Mechanic mech = context.Mechanics.Where(a => a.ID == id).Include(a => a.MechanicAddresses).Include(a => a.MechanicServices).FirstOrDefault();

它 returns 机械师和机械师地址,但机械师服务始终为空(计数 == 0)。

当我 运行 在 LinqPad 中执行相同的查询时,我会按预期填充所有实体。

我删除了 edmx 并重新创建了它,但仍然遇到同样的问题。

请检查 "MultipleActiveResultSets" 是否设置为 true 并且在连接字符串中启用了 LazyLoadingEnabled。可能会有帮助。

那你的 OnModelCreating 呢?

protected override void OnModelCreating(DbModelBuilder modelBuilder)

如果您有 LazyLoading(虚拟),则无需使用 Include。如果它在 LinqPad 中工作正常,请尝试迁移到空数据库(只是测试)。然后尝试从测试数据库中获取数据。

我能够解决这个问题的唯一方法是:

  • 删除 EDMX
  • 为 mechanicsServices 创建脚本 table
  • 编写数据脚本
  • 删除机械服务 table
  • 运行 从上面创建 table 脚本
  • 运行插入数据脚本
  • 重新生成 EDMX

现在可以用了,卧槽!无法解释。

我知道了解哪里出了问题总是最好的,但这一次打败了我。

我遇到了同样的问题。

如果您使用git,请检查旧版本的.edmx 文件。 可能缺少 SSDL 内容