实体框架4 - 特性中的TPT继承CTP5(代码第一):重命名继承表上的外键列 [英] Entity Framework 4 - TPT Inheritance in Features CTP5 (code first): rename foreign key column on inherited table

查看:171
本文介绍了实体框架4 - 特性中的TPT继承CTP5(代码第一):重命名继承表上的外键列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将xml实体框架模型转换为Code First(CTP5)。
我必须建立一个很适合TPT模式的层次结构。
我唯一的问题是继承表的主键/外键与基类的主键具有不同的名称。

I'm trying to convert an xml Entity Framework model to Code First (CTP5) one. I have to model a hierarchy which fits quite well the TPT pattern. The only problem I have is that the primary key/foreign key of the "inheriting" table has a different name from the primary key of the base class.

这些是相关表的相关字段

These are the relevant fields of the involved tables

CREATE TABLE site.Domains
(
    ID INT NOT NULL PRIMARY KEY,
    Domain NVARCHAR(128) NOT NULL
)

CREATE TABLE site.MainSites
(
    FKDomainID INT NOT NULL PRIMARY KEY REFERENCES site.Domains(ID)
)

CREATE TABLE site.SisterSites
(
    FKDomainID INT NOT NULL PRIMARY KEY REFERENCES site.Domains(ID)
)

这是我们在构建模型时使用的代码

this is the code we're using when we build the model

var domain = modelBuilder.Entity<Domain>();

domain.HasKey(c => c.Id)
    .ToTable("Domains", "site");

domain.Property(c => c.DomainName)
    .HasColumnName("Domain")
    .HasMaxLength(128)
    .IsRequired();

domain.Ignore(c => c.OldId);

var mainSite = modelBuilder.Entity<MainSite>();

mainSite.Map(m =>
{
    m.ToTable("MainSites", "site");
});

var sisterSite = modelBuilder.Entity<SisterSite>();

sisterSite.Map(m =>
{
    m.ToTable("SisterSites", "site");
});

这是生成的sql我们得到

and this is the generated sql we get

SELECT 
[Limit1].[C2] AS [C1], 
[Limit1].[C1] AS [C2], 
[Limit1].[Domain] AS [Domain]
FROM ( SELECT TOP (2) 
    [UnionAll1].[Id] AS [C1], 
    [Extent3].[Domain] AS [Domain], 
    CASE WHEN ([UnionAll1].[C1] = 1) THEN ''0X0X'' ELSE ''0X1X'' END AS [C2]
    FROM   (SELECT 
        [Extent1].[Id] AS [Id], 
        cast(1 as bit) AS [C1]
        FROM [site].[MainSites] AS [Extent1]
    UNION ALL
        SELECT 
        [Extent2].[Id] AS [Id], 
        cast(0 as bit) AS [C1]
        FROM [site].[SisterSites] AS [Extent2]) AS [UnionAll1]
    INNER JOIN [site].[Domains] AS [Extent3] ON [UnionAll1].[Id] = [Extent3].[Id]
    WHERE [UnionAll1].[Id] = @p__linq__0
)  AS [Limit1]',N'@p__linq__0 int',@p__linq__0=1

正如你所看到的,它寻找site.MainS ites.Id和site.SisterSites.Id不存在。
我甚至尝试像

As you can see, it looks for site.MainSites.Id and site.SisterSites.Id which do not exist. I even tried something like

mainSite.Property(m => m.Id).HasColumnName("FKDomainID");

但是,你可以猜到,它没有工作。

but, as you can guess, it didn't work.

任何建议?

提前感谢

推荐答案

不幸的是,这在CTP5中是不可能的。这是确认由EF团队

Unfortunately this is not possible in CTP5. This is confirmed by EF team.

这篇关于实体框架4 - 特性中的TPT继承CTP5(代码第一):重命名继承表上的外键列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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