locked
How to map sub sub class with tpt RRS feed

  • Question

  • I had a base abstract class called Party.  There are four derived concrete classes (Company, Person, Department, Position).  There is a property called PartyType in the base class which is used as a discriminator.  My configurations have lines like

    Map<Department>(d => d.Requires("PartyType").HasValue("Department").ToTable("Departments");

    All is good.

    Now I want to add a subclass of Person called Employee.  How to map?  I've tried

    Map<Employee>(e => e.Requires("PartyType").HasValue("Employee").ToTable("Employees");

    but this gives me a runtime error of

    (43,10) : error 3032: Problem in mapping fragments starting at lines 43, 84:EntityTypes WOL.EFData.Person, WOL.EFData.Employee are being mapped to the same rows in table People. Mapping conditions can be used to distinguish the rows that these types are mapped to.

    How do I map this?


    Paul Linton

    Thursday, June 21, 2012 8:00 AM

Answers

  • Hi PaulLinton,

    Welcome to MSDN Forum.

    HasValue method doesn't contain an input parameter. So the mapping statement will not work. Based on this issue, if every entities' PartyType value is fixed, I suggest you to set the value in the constructor of the entity. Please refer to the code below.

    namespace ConsoleApplication38
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (testContext context = new testContext())
                {
                    context.Database.Create();
                }
            }
        }
    
        abstract class Party
        {
            public int id { get; set; }
            public string partyType { get; set; }
        }
    
        class Person:Party
        {
            public Person()
            {
                partyType = "Person";
            }
            public string name { get; set; }
        }
    
        class Department : Party
        {
            public Department()
            {
                partyType = "Department";
            }
            public string name { get; set; }
        }
    
    
        class Employee : Person
        {
            decimal salary { get; set; }
            public Employee()
            {
                partyType = "Employee";
            }
        }
    
        class testContext : DbContext
        {
            public DbSet<Party> partySet { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Party>().ToTable("Parties");
                modelBuilder.Entity<Person>().ToTable("People");
                modelBuilder.Entity<Department>().ToTable("Departments");
                modelBuilder.Entity<Employee>().ToTable("Employees");
            }
        }
    }

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by PaulLinton Friday, June 22, 2012 5:08 AM
    Friday, June 22, 2012 5:06 AM