实体框架不绑定实体

时间:2015-04-15 07:26:14

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

我有以下数据库结构: enter image description here

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

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

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

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你的OnModelCreating怎么样?

protected override void OnModelCreating(DbModelBuilder modelBuilder)

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

答案 2 :(得分:0)

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

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

这现在有效,WTF!无法解释。

我知道最好还是明白出了什么问题,但是这个打败了我。