简体   繁体   English

实体框架中的数据建模和/或查询TPT模型问题

[英]Problems with Data modeling and / or Querying a TPT model in Entity Framework

I have an abstract object which have two list of abstract objects in it. 我有一个抽象对象,其中有两个抽象对象列表。 The Model is being created and the Database looks fine, but I am unable to make the queries I would expect. 正在创建模型,并且数据库看起来很好,但是我无法进行期望的查询。

The Data model looks something like this 数据模型看起来像这样

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; }
}

Any my OnModelCreating in the Context class looks something like this 我在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");

However I am unable to query things like: Get all Vehicles (or concrete classes) with an Incident of Severity of X ie something like this: 但是,我无法查询以下内容:获取所有X严重性事件的车辆(或具体类),例如:

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

The problem is the last part of the query (in the y-part) where I am not able to select Severity, only the Properties of the Abstract class is visible. 问题是查询的最后一部分(在y部分中),其中我无法选择严重性,只有Abstract类的Properties可见。 I am unable to determine (and thus Google) if the problem lies with my Data model or with my Query. 我无法确定问题(因此也无法确定Google)是我的数据模型还是我的查询问题。

Inspired by this: 受此启发:

Issue with many-to-many relationship + TPH inhertitance in Entity Framework 6 实体框架6中的多对多关系+ TPH继承性问题

I got this working. 我工作了。 I removed the specific virtual parts from the Abstract part of my model like this: 我从模型的摘要部分中删除了特定的虚拟部分,如下所示:

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

And changed the navigation properties to 并将导航属性更改为

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

In the OnModelCreating I could remove the two "modelBuilder.Entity().HasMany" lines. 在OnModelCreating中,我可以删除两条“ modelBuilder.Entity()。HasMany”行。 In the end I could execute this query: 最后,我可以执行以下查询:

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

I must admit I am unsure of whether I tried that specific query before, so I am not sure if it is the changes in my Data model that allowed me to make that query or it was possible all along and I just didn't know it. 我必须承认我不确定我之前是否尝试过该特定查询,所以我不确定是否是我的数据模型中的更改使我能够进行该查询,或者一直以来都是可能的,而我只是不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM