我有这个简单的代码:
namespace EFCollectionTest
{
public class Parent
{
public Guid ID { get; set; }
protected internal virtual ICollection<Child1> Children1 { get; set; }
protected internal virtual ICollection<Child2> Children2 { get; set; }
public void AddChild1(Child1 child)
{
Children1.Add(child);
}
public void AddChild2(Child2 child)
{
Children2.Add(child);
}
}
public abstract class Child_Base
{
public Guid ID { get; set; }
}
public abstract class Child_Base<TParent> : Child_Base
where TParent : class
{
public Guid ParentID { get; set; }
public virtual TParent Parent { get; set; }
void SetParent(TParent value)
{
Parent = value;
}
}
public class Child1 : Child_Base<Parent>
{
}
public class Child2 : Child_Base<Parent>
{
}
public class ExampleContext2 : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child1> Children1 { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Parent>().Property(t => t.ID).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Child1>().Property(t => t.ID).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Child2>().Property(t => t.ID).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Child1>()
.Property(t => t.ParentID);
modelBuilder.Entity<Child2>()
.Property(t => t.ParentID);
modelBuilder.Entity<Parent>()
.HasMany(t => t.Children1)
.WithRequired(t => t.Parent)
.HasForeignKey(d => d.ParentID);
modelBuilder.Entity<Parent>()
.HasMany(t => t.Children2)
.WithRequired(t => t.Parent)
.HasForeignKey(d => d.ParentID);
}
}
}
这种情况看起来非常简单,一个Parent有一个Child1集合和一个Child2集合。
问题是,当实体框架构建其模型时,抛出此异常:
在模型生成期间检测到一个或多个验证错误: 父级:FromRole:NavigationProperty“父级”无效。在AssociationType'Parent_Children2'中输入FromRole'Parent_Children2_Target'的'Child2'必须与声明此NavigationProperty的类型'Child1'完全匹配。
完整堆栈跟踪是:
System.Data.Entity.ModelConfiguration.ModelValidationException:
One or more validation errors were detected during model generation:
Parent: FromRole: NavigationProperty 'Parent' is not valid.
Type 'Child2' of FromRole 'Parent_Children2_Target' in AssociationType
'Parent_Children2' must exactly match with the type 'Child1' on which this
NavigationProperty is declared on.
at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
这个映射有什么问题,因为它似乎非常直接,并且显然没有关于Child1 / Child2的异常声明这样的混淆?