代码第一个问题是父子和继承映射
本文关键字:继承 映射 父子 第一个 问题是 代码 | 更新日期: 2023-09-27 17:49:32
我遇到了继承映射的一些问题,我不能让它正确映射到基类,并在基类(元素)的所有字段上获得无效列
在我们从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");
});
}
Error message i receive:
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
您的映射完全错误。数据库中每个实体(包括基本实体类型Elements
)都有单独的表。这意味着您必须使用每类型表(TPT)映射,但您的代码正在使用每具体类型表(TPC)映射。TPC要求基类型的表不存在,而所有派生实体的表具有所有基类型的列。这就是为什么会出现异常的原因。从所有子映射中删除m.MapInheritedProperties();