Entity Framework 中数据建模和/或查询 TPT 模型的问题
Problems with Data modeling and / or Querying a TPT model in Entity Framework
我有一个抽象对象,其中有两个抽象对象列表。正在创建模型并且数据库看起来不错,但我无法进行我期望的查询。
数据模型看起来像这样
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; }
}
上下文中的任何我的 OnModelCreating class 看起来像这样
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 的车辆(或具体 classes),即类似这样的内容:
var problems = context.Vehicle.Where(x => x.CrashIncidents.Any(y => y.Severity == "High");
问题是查询的最后一部分(在 y 部分)我无法 select 严重性,只有摘要的属性 class 可见。我无法确定(因此 Google)问题出在我的数据模型还是我的查询上。
受此启发:
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");
我必须承认我不确定我之前是否尝试过该特定查询,所以我不确定是我的数据模型中的更改允许我进行该查询还是一直有可能,我只是不知道。
我有一个抽象对象,其中有两个抽象对象列表。正在创建模型并且数据库看起来不错,但我无法进行我期望的查询。
数据模型看起来像这样
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; }
}
上下文中的任何我的 OnModelCreating class 看起来像这样
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 的车辆(或具体 classes),即类似这样的内容:
var problems = context.Vehicle.Where(x => x.CrashIncidents.Any(y => y.Severity == "High");
问题是查询的最后一部分(在 y 部分)我无法 select 严重性,只有摘要的属性 class 可见。我无法确定(因此 Google)问题出在我的数据模型还是我的查询上。
受此启发:
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");
我必须承认我不确定我之前是否尝试过该特定查询,所以我不确定是我的数据模型中的更改允许我进行该查询还是一直有可能,我只是不知道。