简体   繁体   English

EF 4.1父级子代和继承映射的代码优先问题

[英]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() 在从CTP5升级到4.1以及使用.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 ). 您的数据库为每个实体(包括基本实体类型Elements )都有一个单独的表。 It means that you have to use Table-per-Type (TPT) mapping but your code is using Table-Per-Concrete Type (TPC) mapping. 这意味着您必须使用每类型表(TPT)映射,但是您的代码正在使用每具体表类型(TPC)映射。 TPC requires that table for base type does not exists and instead tables for all derived entities have all base type's columns. TPC要求不存在基本类型的表,而是所有派生实体的表都具有所有基本类型的列。 That is the reason why you cat that exception. 这就是您选择该例外的原因。 Remove m.MapInheritedProperties(); 删除m.MapInheritedProperties(); from all your child mappings. 从您所有的孩子映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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