EF 4.1 Code父代子代和继承映射的第一个问题

时间:2011-04-06 21:38:00

标签: c# inheritance entity-framework-4.1 fluent-interface database-first

我遇到了继承映射的一些问题,我无法将其映射到基类,并在基类的所有字段中获取无效列(元素)

在我们从CTP5升级到4.1并使用.IsIndependent()

之前,项目工作正常

我的代码如下:

表格结构:

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,
)

映射;

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");
    });
}

我回复的错误信息:

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

1 个答案:

答案 0 :(得分:1)

您的映射完全错误。您的数据库为每个实体都有单独的表(包括基本实体类型Elements)。这意味着您必须使用Table-per-Type(TPT)映射,但您的代码使用Table-Per-Concrete Type(TPC)映射。 TPC要求基本类型的表不存在,而所有派生实体的表都具有所有基本类型的列。这就是你捕获这个例外的原因。从您的所有子映射中删除m.MapInheritedProperties();