实体框架中的条件映射 - 与TPH的OR运算 [英] Conditional Mapping in Entity Framework - OR operation with TPH

查看:201
本文介绍了实体框架中的条件映射 - 与TPH的OR运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个实体,如 Vehicle 和三个衍生实体,如 Car 摩托车自行车
此继承层次结构使用 TPH 实现。

We have an entity like Vehicle and three derived entities such as Car, Motorbike and Bicycle. This inheritance hierarchy is implemented with TPH.

以下是实体映射条件:


  • __ disc__ =汽车汽车

  • __ disc__ =摩托车为摩托车

  • __ disc__ =自行车为自行车

  • __disc__ = car for Car
  • __disc__ = motorbike for Motorbike
  • __disc__ = bicycle for Bicycle

我如何 车辆派生另一个孩子,如 MotorVehicle 具有以下映射条件:

How can I derive another child from Vehicle like MotorVehicle with following mapping condition:

__ disc__ = MotorVehicle的汽车或摩托车 / p>

__disc__ = car OR motorbike for MotorVehicle

当我使用 TPT 这个结构时,我会在数据库中查看: / p>

I'd view in Database like this when I had this structure with TPT:

SELECT    Id
FROM      Vehicles
WHERE     (__Disc__ = N'car') OR (__Disc__ = N'motorbike')

我认为TPH不需要此视图。

I think this view is not required with TPH.

请注意, 我不能像这样改变继承:车辆 - 汽车。不要以为汽车和摩托车和自行车已经存在的汽车和其他孩子的父母为汽车注射汽车。

Please note that I can not change the inheritance like this: Vehicle<-- MotorVehicle<-- Car. Don't think about injecting the motor vehicles as the parent of car and other children because Car and Motorbike and Bicycle are already exists. I just want to assign some business to all motor vehicles.

推荐答案

为什么你不能介绍一个在类层次结构中的MotorVehicle 级别?您可以。它只是一个抽象类,所以它不需要一个鉴别器的值。 EF几乎没有注意到这个类!

Why couldn't you introduce a MotorVehicle level in the class hierarchy? You can. It's just an abstract class, so it doesn't need a discriminator value. EF hardly notices the class!

我尝试使用和不使用 MotorVehicle 类和数据库结构和数字在这两种情况下,定义的歧视者是相同的。

I tried both with and without the MotorVehicle class and the database structure and the number of defined discriminators was the same in both cases.

修改

这是我做的:

public abstract class Vehicle
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public abstract class MotorVehicle : Vehicle
{
    public int Hp { get; set; }
}

public class Car : MotorVehicle
{ }

public class MotorBike : MotorVehicle
{ }

public class Bicycle : Vehicle
{ }

internal class NestedInheritanceContext : DbContext
{
    public DbSet<Vehicle> Vehicles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Vehicle>().Property(v => v.Name).IsRequired();
        modelBuilder.Entity<Car>().Map(m => m.Requires("Discriminator")
            .HasValue("car").HasColumnType("char")
            .HasMaxLength(10)
            .IsRequired());
        modelBuilder.Entity<MotorBike>().Map(m => m.Requires("Discriminator")
            .HasValue("motorbike"));
        modelBuilder.Entity<Bicycle>().Map(m => m.Requires("Discriminator")
            .HasValue("bicycle"));
        base.OnModelCreating(modelBuilder);
    }
}

这篇关于实体框架中的条件映射 - 与TPH的OR运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆