코드를 먼저 사용하고 수동 마이그레이션을 통해 Entity Framework 4.3을 사용하고 있습니다. 두 개의 사용자 지정 discriminator 필드를 사용하는 TPH (table-per-hierarchy) 설치를 매핑하려고합니다. 하나는 판별 자 자체이고 다른 하나는 소프트 삭제 (NH 클래스 매핑의 "where"옵션과 유사)입니다. 정확한 설정은 EF 4.2에서 실행되는 다른 프로젝트에서도 잘 작동합니다.Entity Framework 4.3 - TPH 매핑 및 마이그레이션 오류
NuGet 콘솔에서 "add-migration"명령을 사용하여 마이그레이션을 추가하려고 할 때 오류가 발생합니다. "OnModelCreating"메서드, EntityTypeConfiguration 클래스 등에서 클래스의 특성을 정의하는 모든 조합을 시도했습니다. 복잡한 계층 매핑을 포함하지 않은 이전의 마이그레이션은 정상적으로 작동했습니다.
EF 4.3에 몇 가지 중요한 변경 사항이 있습니까?
코드 :
//---- Domain classes ---------------------
public abstract class ParentClass
{
public string ParentString { get; set; }
}
public class Foo : ParentClass
{
public string FooString { get; set; }
}
public class Bar : ParentClass
{
public string BarString { get; set; }
}
//---- Mapping configuration --------------
public class ParentConfiguration : EntityTypeConfiguration<ParentClass>
{
public ParentConfiguration()
{
Map<Foo>(m =>
{
m.Requires("IsActive").HasValue(1);
m.Requires("Type").HasValue("Foo");
})
.ToTable("Parent");
Map<Bar>(m =>
{
m.Requires("IsActive").HasValue(1);
m.Requires("Type").HasValue("Bar");
})
.ToTable("Parent");
}
}
//---- Context ----------------------------
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ParentConfiguration());
}
오류 :
System.InvalidOperationException: The type 'Foo' has already been mapped to table 'Parent'. Specify all mapping aspects of a table in a single Map call.
at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.AddMappingConfiguration(EntityMappingConfiguration mappingConfiguration)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ReassignSubtypeMappings()
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(Action`1 writeXml)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(DbContext context)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at System.Data.Entity.Migrations.Design.ToolingFacade.GetPendingMigrationsRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
미켈
고마워요, 제가 원했던 것입니다. 그리고 이상한 방식으로, 각 하위 유형에 대한 하나의 구성이 옳은 것으로 느껴집니다. 이전 버전과의 호환성은 좋았지 만 향후 릴리스에서 수정 될 수 있기를 바랍니다. – Mihkel
이것은 3 년 늦었지 만,'Map (m => m.Requires ...')은'ToTable ("테이블 이름")을 호출하기 전에 ** 작업이 완료되면 작동 할 것입니다. Entity Framework 6.1.3 btw. –