Tôi đang sử dụng Entity Framework 4.3 với mã di chuyển đầu tiên và thủ công. Tôi đang cố gắng để bản đồ một thiết lập TPH (table-per-hierarchy) trong đó sử dụng hai lĩnh vực phân biệt đối xử tùy chỉnh. Một cho chính phân biệt đối xử và phần còn lại cho xóa mềm (giống như tùy chọn "where" trong ánh xạ lớp NH). Thiết lập chính xác tương tự chỉ hoạt động tốt trong một dự án khác chạy trên EF 4.2.Khuôn khổ thực thể 4.3 - Lỗi lập bản đồ và di chuyển TPH
Tôi gặp lỗi khi cố gắng thêm di chuyển bằng cách sử dụng lệnh "thêm di chuyển" trong bảng điều khiển NuGet. Tôi đã thử tất cả các kết hợp của việc xác định tên bảng - thuộc tính trên lớp, trong "OnModelCreating" phương pháp, trong các lớp EntityTypeConfiguration vv Di chuyển trước đó của tôi mà không liên quan đến ánh xạ phân cấp phức tạp đã làm việc tốt.
Có một số thay đổi đột phá trong EF 4.3 mà tôi đã tình cờ gặp phải không?
Mã:
//---- 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());
}
Lỗi:
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
Cảm ơn bạn, điều này chỉ là những gì tôi muốn. Và theo một cách kỳ lạ cảm thấy đúng - rõ ràng hơn - một cấu hình cho mỗi loại phụ. Mặc dù khả năng tương thích ngược sẽ tốt đẹp và tôi hy vọng điều này sẽ được khắc phục trong các bản phát hành trong tương lai. – Mihkel
Đây là trễ 3 năm, nhưng các cuộc gọi tới 'Bản đồ (m => m.Requires ...' sẽ chỉ hoạt động nếu nó được thực hiện ** TRƯỚC KHI ** cuộc gọi đến 'ToTable (" tên bảng ")'. Khuôn khổ thực thể 6.1.3 btw. –