开发者

Entity Framework 4 - TPT Inheritance in Features CTP5 (code first): rename foreign key column on inherited table

开发者 https://www.devze.com 2023-02-10 17:30 出处:网络
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.

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

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 
开发者_Python百科        [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

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.

Any suggestion?

Thanks in advance


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

0

精彩评论

暂无评论...
验证码 换一张
取 消