我可以使用每个类型的表映射来指定一个鉴别符列吗? [英] Can I specify a discriminator column with a table-per-type mapping?

查看:93
本文介绍了我可以使用每个类型的表映射来指定一个鉴别符列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类层次结构,我想使用Entity Framework 4.1 Code First在多个表之间进行映射.就像每个类型的表格(TPT),但我也想要一个区分列.

I have a class hierarchy that I want to map across several tables using Entity Framework 4.1 Code First. It's like table-per-type (TPT) but I also want a discrimator column.

层次结构如下:

public class Event
{
    public Guid Id { get; set; }
    public string Code { get; set; } // discriminator
    public DateTime Date { get; set; }
}

public class Party : Event
{
    public int AttendeeCount { get; set; }
}

public class BirthdayParty : Party
{
    public int Age { get; set; }
}

public class WeddingParty : Party
{
    public string Surname { get; set; }
}

这是一个很弱的例子,但我希望它是有道理的.将有一个事件"表,一个聚会"表以及每种聚会的表.但是,区分项列(代码")对于每种事件都有一个已知值,例如生日派对的生日"或婚礼派对的婚礼".

This is a pretty weak example but I hope it makes sense. There'll be an "Events" table, a "Parties" table and a table for each kind of party. However, the discriminator column ("Code") will have a known value for each kind of event, like "BIRTH" for birthday parties or "WEDDING" for wedding parties.

这个想法是,如果我只查询给定日期的生日聚会,EF就会知道在查询中添加Code = 'BIRTH',而不是执行一堆UNION和JOIN来确定需要的行.

The idea is that if I query for just birthday parties on a given date, EF would know to add Code = 'BIRTH' to my query instead of doing a bunch of UNIONs and JOINs to work out which rows it needs.

我这样映射我的最低级别的课程:

I map my lowest-level classes like this:

var bd = modelBuilder.Entity<BirthdayParty>();
bd.ToTable("BirthdayParties");
bd.Property(p => p.Age).HasColumnName("BirthdayAge");

我现在需要以某种方式在其中指定鉴别值.我已经尝试过了:

I now need to specify the discriminator value in there somehow. I've tried this:

modelBuilder.Entity<Event>().Map<BirthdayParty>(cfg =>
    {
        cfg.Requires("Code").HasValue("BIRTH");
    });

...但是这抱怨我没有在对Map的调用中指定表名.所以我尝试将ToTable调用移到那里:

... but that complains that I haven't specified the table name inside the call to Map. So I tried moving the ToTable call into there:

var bd = modelBuilder.Entity<BirthdayParty>();
bd.Property(p => p.Age).HasColumnName("BirthdayAge");

modelBuilder.Entity<Event>().Map<BirthdayParty>(cfg =>
    {
        cfg.Requires("Code").HasValue("BIRTH");
        cfg.ToTable("BirthdayParties");
    });

...,现在它认为我想要"BirthdayParties"表中的代码"列,这是不正确的.我已经告诉过它事件"表中的代码"列.

... and now it thinks I want a "Code" column in the "BirthdayParties" table, which is not correct. I've already told it that the "Code" column is in the "Events" table.

这甚至有可能吗?我可以将区分符列的使用与每个类型的表的映射结合起来吗?

Is this even possible? Can I combine the use of a discriminator column with a table-per-type mapping?

推荐答案

不幸的是,不支持此功能. 鉴别符"列只能在TPH中使用. TPT的实体类型因映射表而异,并且它总是产生那些糟糕的查询.它可能是一个不错的功能,因此可能对数据UserVoice提出建议使其实现一天.

Unfortunately this is not supported. Discriminator column can be used only in TPH. TPT differs entity types by mapped tables and it always produces those terrible queries. It could be nice feature so perhaps suggestion on Data UserVoice would make it implemented one day.

更新

已经有关于此名称的用户语音建议"

There is already a suggestion on user voice for this titled "Discriminator column support in TPT inheritance".

这篇关于我可以使用每个类型的表映射来指定一个鉴别符列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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