Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework 4 - TPT Inheritance in Features CTP5 (code first): rename foreign key column on inherited table
    primarykey
    data
    text
    <p>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.</p> <p>These are the relevant fields of the involved tables</p> <pre><code>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) ) </code></pre> <p>this is the code we're using when we build the model</p> <pre><code>var domain = modelBuilder.Entity&lt;Domain&gt;(); domain.HasKey(c =&gt; c.Id) .ToTable("Domains", "site"); domain.Property(c =&gt; c.DomainName) .HasColumnName("Domain") .HasMaxLength(128) .IsRequired(); domain.Ignore(c =&gt; c.OldId); var mainSite = modelBuilder.Entity&lt;MainSite&gt;(); mainSite.Map(m =&gt; { m.ToTable("MainSites", "site"); }); var sisterSite = modelBuilder.Entity&lt;SisterSite&gt;(); sisterSite.Map(m =&gt; { m.ToTable("SisterSites", "site"); }); </code></pre> <p>and this is the generated sql we get</p> <pre><code>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 </code></pre> <p>As you can see, it looks for site.MainSites.Id and site.SisterSites.Id which do not exist. I even tried something like</p> <pre><code>mainSite.Property(m =&gt; m.Id).HasColumnName("FKDomainID"); </code></pre> <p>but, as you can guess, it didn't work.</p> <p>Any suggestion?</p> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload