I have an table hierarchy with Table per Concrete Type (TPC) mapping. (I not sure that [Key, Column] attribute are necessary but according https://stackoverflow.com/a/19792915/959779 it's should work)
Class structure:
public abstract class BaseEntity
{
[Key, Column(Order = 0)]
public int Id { get; set; }
}
public class Page: BaseEntity {
string Data { get; set; }
}
public class PageBackup: Page {
[Key, Column(Order = 1)]
public int PageId { get; set; }
public virtual Page Page { get; set; }
}
WhenId
of the Page
and PageBackup
entities are intersect(after adding to tables some items) EF throw the next excution
"All objects in the EntitySet 'EUICDataContext.Pages' must have unique primary keys. However, an instance of type 'Entities.PageBackup' and an instance of type 'Entities.Page' both have the same primary key value, 'EntitySet=Pages;Id=6'. "
Entities mapping code below:
class PageMapping : EntityTypeConfiguration<Page>
{
public PageMapping ()
{
Map(m =>{ m.ToTable("Page"); });
HasKey(t => t.Id });
}
}
class PageBackupMapping : EntityTypeConfiguration<PageBackup>
{
public PageBackupMapping ()
{
Map(m =>{
m.MapInheritedProperties();
m.ToTable("PageBackup");
});
HasKey(t => new { t.Id, t.PageId });
}
}
The problem is although in the EF PageMapping
has composite key Update-Database
seed script creates table with the next signature:
CREATE TABLE [dbo].[PageBackup](
[Id] [int] IDENTITY(1,1) NOT NULL,
[PageId] [int] NOT NULL,
[Data] [nvarchar](max) NULL,
CONSTRAINT [PK_dbo.PageBackup] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
Is EF allows to create tables with composite keys(lot of examples with mapping but I didn't found any about the migrations)?
And how to describe such configuration on behalf of TPC mapping?
Any other solution proposal will be helpful.