0

Simple test project working from examples in Julia Lerman's EF Code First book. Not a single example works TPH, TPT or TPC when using the Map method, but fine without. Every other aspect of EF (I have a part developed application) appears to work okay. I'm using VS 2010/.NET 4 with all latest updates and I even resorted to repairing the VS installation today.

The following TPH example throws an InvalidOperationException - "Map was called more than once for type 'Child' and at least one of the calls didn't specify the target table name."

using System.Linq;
using System.Data.Entity;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<Context>());
            var children = new Context().Set<Child>().ToList();
        }
    }

    public class Parent
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Child : Parent
    {
    }

    public class Context : DbContext
    {
        DbSet<Parent> Parents { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Parent>()
                .Map(m =>
                    {
                        m.Requires("EntityType").HasValue("Parent");
                        m.ToTable("Families");
                    })
            .Map<Child>(m => m.Requires("EntityType").HasValue("Child"));
        }
    }
}

Another example, TPT this time, throws "The type 'Child' has already been mapped to table 'Children'. Specify all mapping aspects of a table in a single Map call."

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Parent>()
                .Map(m => m.ToTable("Families"))
                .Map<Child>(m => m.ToTable("Children"));
        }

The final example, TPC, throws similar "The type 'Child' has already been mapped to table 'Children'. Specify all mapping aspects of a table in a single Map call."

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Parent>()
                .Map(m => m.ToTable("Families"))
                .Map<Child>(m =>
                    {
                        m.ToTable("Children");
                        m.MapInheritedProperties();
                    });
        }

I've tried any number of other examples too with similar results. Either I'm missing something very basic or I suspect I have an installation/configuration problem. My PC also has VS2008 installed and a few months ago I did a lot of re-configuring SQL Server as I had 4 different versions installed. I'm now using 2008 R2 Express.

Has anybody seen similar or have any idea how I can trace/debug what's going on in EF?

NickM
  • 1
  • 2

1 Answers1

0

Found the answer here... Entity Framework 4.3 - TPH mapping and migration error

... This is a known issue with 4.3 and 4.3.1. (We found it too late to put the fix in 4.3.1.)

... In a nutshell, you used to be able to make chained map calls on a single EntityConfiguration in 4.1. and 4.2

Thankfully, the suggested alternative way of calling Map (ie not chaining) fixed all my issues.

Community
  • 1
  • 1
NickM
  • 1
  • 2