【问题标题】:Entity Framework not binding entity实体框架不绑定实体
【发布时间】:2015-06-21 01:10:27
【问题描述】:

我有以下数据库结构:

我正在使用 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();

它返回 mechanic 和 mechanicAddresses 但 mechanicServices 始终为空(计数 == 0)。

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

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

【问题讨论】:

  • 谢谢史蒂夫,已经完成了,我运行分析器来执行实际的查询。 SQL按预期加入表,当我对数据库运行它时,数据就在结果中。它只是没有从结果到实体模型。
  • 是否 var mechService = context.MechanicServices.Where(a => a.MechanicId == id).FirstOrDefault();返回结果?
  • 嗨史蒂夫,是的,奇怪的是它确实如此。我已经根据我在下面添加的答案设法解决了这个问题,但最好不要重新生成 SQL 表和 EDMX。哦,它是固定的(现在。)。感谢您的建议。

标签: linq entity-framework linq-to-entities


【解决方案1】:

我有同样的问题。

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

【讨论】:

    【解决方案2】:

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

    • 删除 EDMX
    • 为mechanismsServices 表创建脚本
    • 编写数据脚本
    • 删除mechanismsServices 表
    • 从上面运行创建表脚本
    • 运行插入数据脚本
    • 重新生成 EDMX

    现在可以了,WTF!无法解释。

    我知道最好了解哪里出了问题,但这个问题打败了我。

    【讨论】:

      【解决方案3】:

      你的 OnModelCreating 呢?

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      

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

      【讨论】:

      • 谢谢亚历山大,但我已经迁移了数据库并测试了同样的事情。由于某种原因,它没有获得 MechanicsServices,但它会很高兴地获得所有其他关系。与 EF 解决此问题的方式有关。我只是看不出有什么问题。
      【解决方案4】:

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

      【讨论】:

      • 您好 Dreamweaver,MultipleActiveResultSets 在连接字符串中为 True。 linQ 正在使用“包含”,它指定我想要急切加载而不是懒惰。我已经使用 EF6 编写了 100 条 linQ 查询,但从未遇到过这个问题,事实上它在 LinqPad 中运行良好,而不是通过 EF,这告诉我这是与 EF 相关的,但数据库结构和实体对我来说看起来不错。
      猜你喜欢
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多