I am trying to convert an Entity Framework xml model to Code First (CTP5). I need to model a hierarchy that is quite consistent with the TPT pattern. The only problem I encountered is that the primary key / foreign key of the inheritance table has a different name from the primary key of the base class.
These are the relevant fields of the tables involved.
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 use 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 that 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
As you can see, it is looking for site.MainSites.Id and site.SisterSites.Id that do not exist. I even tried something like
mainSite.Property(m => m.Id).HasColumnName("FKDomainID");
but, as you can guess, this did not work.
Any suggestion?
Thanks in advance