刚刚把EF版本从6.0.2升级到了6.1.0,没想到系统运行竟然出问题了。
我写了一份测试代码,下面代码就是我的所有代码:
using System; using System.Data.Entity; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { using (MyContext context = new MyContext()) { bool b = context.Database.CreateIfNotExists(); MyClassToA toA = new MyClassToA() { A = "A" }; context.MyClasses.Add(toA); MyClassToB toB = new MyClassToB() { B = "B" }; context.MyClasses.Add(toB); context.SaveChanges(); } } } public abstract class MyClass { public MyClass() { this.Id = Guid.NewGuid(); } public Guid Id { get; private set; } public string Name { get; set; } } public class MyClassToA : MyClass { public string A { get; set; } } public class MyClassToB : MyClass { public string B { get; set; } } public sealed class MyContext : DbContext { public DbSet<MyClass> MyClasses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<MyClass>().Map<MyClassToA>(m => m.Requires("Discriminator").HasValue("A").IsRequired()); modelBuilder.Entity<MyClass>().Map<MyClassToB>(m => m.Requires("Discriminator").HasValue("B").IsRequired()); } } }
这份代码在EF6.0.2下,没有问题,但是到Ef6.1.0,执行到SaveChanges()时,就出现下面问题:
{"\r\n(6,10) : error 3032: Problem in mapping fragments starting at lines 6, 12:EntityTypes ConsoleApplication1.MyClassToA, ConsoleApplication1.MyClassToB are being mapped to the same rows in table MyClass. Mapping conditions can be used to distinguish the rows that these types are mapped to.\r\n"}
是我哪里用得不对吗?求解中。。。
看问题说明似乎是说两个类映射到同一张表下了?你应该一张表对应一个类
是采用的TPH继承关系,MyClassToA,MyClassToB都是MyClass的子类,都映射到MyClasses表。