Search code examples
xafef-code-first-mapping

Can I have EF code first TPH with a non abstract base class?


In have a table in SQL Server defined by

    CREATE TABLE [dbo].[ReportDataV2](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [DataTypeName] [nvarchar](max) NULL,
    [IsInplaceReport] [bit] NOT NULL,
    [PredefinedReportTypeName] [nvarchar](max) NULL,
    [Content] [varbinary](max) NULL,
    [DisplayName] [nvarchar](max) NULL,
    [ParametersObjectTypeName] [nvarchar](max) NULL,
 CONSTRAINT [PK_dbo.ReportDataV2] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

which happens to be the Dev Express XAF Report table.

In my data context I have

public DbSet<ReportDataV2> ReportDataV2 { get; set; }

I want to be able to treat the DataTypeName field as a discriminator column without interfering with the way ReportDataV2 already works in my code.

I tried the following, but Entity Framework detects that the data structure has changed, and if I generate the migration I see that it is trying to recreate the ReportDataV2 table.

public class OrderCountReport2Configuration : EntityTypeConfiguration<ReportDataV2>
{
    public OrderCountReportConfiguration()
        : base()
    {
         ToTable("ReportDataV2", "dbo");

         HasKey(tp => tp.ID);

        Map<OrderCountReport>(m => m.Requires("DataTypeName").HasValue("OrderCountReport"));

    }
}

   protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new OrderCountReportConfiguration());
        base.OnModelCreating(modelBuilder);
    }
}

Solution

  • The discriminator column must have a size limit , just like an indexed column needs a size limit