【问题标题】:Problems with Data modeling and / or Querying a TPT model in Entity Framework在实体框架中数据建模和/或查询 TPT 模型的问题
【发布时间】:2015-12-15 08:27:25
【问题描述】:

我有一个抽象对象,其中有两个抽象对象列表。正在创建模型并且数据库看起来不错,但我无法进行预期的查询。

数据模型看起来像这样

public abstract class Vehicle
{
    protected Vehicle() 
    {
        this.CrashIncidents = new List<Incident>();
        this.SpeedingIncidents = new List<Incident>();
    }
    [Key]
    public int Id { get; set; }
    public virtual ICollection<Incident> CrashIncidents { get; set; }
    public virtual ICollection<Incident> SpeedingIncidents { get; set; }
}

public class Car : Vehicle
{
    public string Color { get; set; }
}

public class Lorry : Vehicle
{
    public int MaxCarryWeight { get; set; }
}

public abstract class Incident
{
    [Key]
    public int Id { get; set; }
    public virtual ICollection<Incident> VehicleCrashIncidents { get; set; }
    public virtual ICollection<Incident> VehicleSpeedingIncidents { get; set; }
}

public class CrashIncident : Incident
{
    public string Severity { get; set; }
}

public class SpeedingIncident : Incident
{
    public string MPHRegistered { get; set; }
}

我在 Context 类中的任何 OnModelCreating 看起来都像这样

modelBuilder.Entity<Vehicle>().HasMany<Incident>(o => o.CrashIncident).WithMany(a => a.VehicleCrashIncidents).Map(m => m.MapLeftKey("Id").MapRightKey("VehicleCrashIncidentId").ToTable("VehicleCrashIncident"));
modelBuilder.Entity<Vehicle>().HasMany<Incident>(o => o.SpeedingIncident).WithMany(a => a.VehicleSpeedingIncidents).Map(m => m.MapLeftKey("Id").MapRightKey("VehicleSpeedingIncidentId").ToTable("VehicleSpeedingIncident"));

modelBuilder.Entity<CrashIncident>().ToTable("CrashIncident");
modelBuilder.Entity<SpeedingIncident>().ToTable("SpeedingIncident");

但是我无法查询以下内容:获取所有具有 X 严重性事件的车辆(或具体类),例如:

var problems = context.Vehicle.Where(x => x.CrashIncidents.Any(y => y.Severity == "High");

问题是查询的最后一部分(在 y 部分)我无法选择严重性,只有抽象类的属性可见。我无法确定(因此也无法确定 Google)问题出在我的数据模型还是我的查询上。

【问题讨论】:

    标签: c# entity-framework linq data-modeling


    【解决方案1】:

    受此启发:

    Issue with many-to-many relationship + TPH inhertitance in Entity Framework 6

    我得到了这个工作。我从模型的抽象部分中删除了特定的虚拟部分,如下所示:

    public abstract class Vehicle
    {
        protected Vehicle() 
        {
            this.Incidents= new List<Incident>();
        }
        [Key]
        public int Id { get; set; }
        public virtual ICollection<Incident> Incidents{ get; set; }
    }
    

    并将导航属性更改为

    public abstract class Incident
    {
        [Key]
        public int Id { get; set; }
        public virtual ICollection<Incident> VehicleIncidents { get; set; }
    }
    

    在 OnModelCreating 中,我可以删除两个“modelBuilder.Entity().HasMany”行。最后我可以执行这个查询:

    var problems = context.Vehicle.Where(x => x.Incidents.OfType<CrashIncidents>.Any(y => y.Severity == "High");
    

    我必须承认我不确定我之前是否尝试过该特定查询,所以我不确定是我的数据模型中的更改允许我进行该查询还是一直有可能,而我只是没有不知道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 2011-03-19
      相关资源
      最近更新 更多