简体   繁体   中英

EF 4.1 Code First problem with parent child and Inheritance Mapping

I have run into some problems with Inheritance Mapping, I can't get it to map correct to the base class, and get an Invalid column on all the fields in the base class (Elements)

The project wos working before we upgraded from CTP5 to 4.1 and where using the .IsIndependent()

My code look like this:

Table structure:

CREATE TABLE [dbo].[elements](
    [elementID] [uniqueidentifier] NOT NULL,
    [elementElementID] [uniqueidentifier] NULL,
    [name] [nvarchar](50) NOT NULL,
    [solutionID] [int] NOT NULL,
    [elementTypeID] [int] NOT NULL,
    [dateCreate] [datetime] NOT NULL,
    [dateChange] [datetime] NOT NULL,
    [placeholderNumber] [int] NULL
)

CREATE TABLE [dbo].[elementRoots](
    [elementID] [uniqueidentifier] NOT NULL,
    [allowsiteCounts] [int] NOT NULL
)


CREATE TABLE [dbo].[elementSites](
    [elementID] [uniqueidentifier] NOT NULL,
    [languageCode] [nvarchar](5) NOT NULL
)


CREATE TABLE [dbo].[elementPages](
    [elementID] [uniqueidentifier] NOT NULL,
    [elementMasterID] [uniqueidentifier] NULL,
    [title] [nvarchar](50) NOT NULL,
    [desciption] [nvarchar](255) NULL,
    [path] [nvarchar](512) NULL,
)

Mapping;

public DbSet<Element> Elements { get; set; }
public DbSet<ElementRoot> ElementRoots { get; set; }
public DbSet<ElementSite> ElementSites { get; set; }
public DbSet<ElementPage> ElementPages { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{


    modelBuilder.Entity<Element>().HasKey(x => x.elementId);

    modelBuilder.Entity<Element>()
        .HasOptional(s => s.Parent)
        .WithMany(c => c.Children)
        .HasForeignKey(s => s.elementElementId);

        modelBuilder.Entity<Element>().ToTable("elements");

    modelBuilder.Entity<ElementRoot>().Map(m =>
    {

        m.MapInheritedProperties();
        m.ToTable("elementRoots");

    });

    modelBuilder.Entity<ElementSite>().Map(m =>
    {
        m.MapInheritedProperties();
        m.ToTable("elementSites");

    });

    modelBuilder.Entity<ElementPage>().Map(m =>
    {
        m.MapInheritedProperties();
        m.ToTable("elementPages");
    });
}

Error message i recive:

Invalid column name 'solutionID'.
Invalid column name 'name'.
Invalid column name 'solutionID'.
Invalid column name 'elementTypeID'.
Invalid column name 'dateCreate'.
Invalid column name 'dateChange'.
Invalid column name 'elementElementId'.
Invalid column name 'placeholderNumber'.
Invalid column name 'solutionID'.
Invalid column name 'name'.
Invalid column name 'solutionID'.
Invalid column name 'elementTypeID'.
Invalid column name 'dateCreate'.
Invalid column name 'dateChange'.
Invalid column name 'elementElementId'.
Invalid column name 'placeholderNumber'.
Invalid column name 'solutionID'.
Invalid column name 'name'.
Invalid column name 'solutionID'.
Invalid column name 'elementTypeID'.
Invalid column name 'dateCreate'.
Invalid column name 'dateChange'.
Invalid column name 'elementElementId'.
Invalid column name 'placeholderNumber'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2030802
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009584
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
   System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
   System.Data.SqlClient.SqlDataReader.get_MetaData() +86
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +311
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
   System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10
   System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +443

Your mapping is completely wrong. Your database has separate table for each entity (including the base entity type Elements ). It means that you have to use Table-per-Type (TPT) mapping but your code is using Table-Per-Concrete Type (TPC) mapping. TPC requires that table for base type does not exists and instead tables for all derived entities have all base type's columns. That is the reason why you cat that exception. Remove m.MapInheritedProperties(); from all your child mappings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM