none
EF5 Two level inheritance, TPT + TPH (Code first). Is it possible? RRS feed

  • Question

  • I've implemented a simple inheritance using EF5 code-first TPT. I won't post my schema as would require explanations but suppose my base class is "Person" and my inherited class is "User". Tables are named accordingly.

    Due to the fact my software is gonna be reused as a framework for other people to build on top of it, I want to give them an easy tool to expand the software without changing the database. I need only to fetch the right class type, I won't need to update it. In order to achieve this I want a specialized layer for Users with TPH. Developers will add their class in code and insert their record marking the type.

    I've added to Users table the "Discriminator" field but now I'm getting this error trying to load the model:

    Error 3032: Problem in mapping fragments : mapped to the same rows in table

    It's not really clear what the error means.. Can anyone suggest an explanation/solution for it?

    Is it possible?

    Thanks in advance




    • Edited by DaniloC Friday, August 30, 2013 12:48 PM
    Friday, August 30, 2013 12:10 PM

Answers

  • Hello DaniloC,

    Thank you for posting your question to this forum.

    From your description, you want to know why it throws an exception when you are trying to load the model.

    If I have misunderstood, please let me know.

    As far as I know, if we want to use Code-First TPH, we need to set the same column name for Persons and Users like codes below:

    modelBuilder.Entity<Person>().Map<Person>(m =>
                {
                    m.ToTable("Lodgings");
                    m.Requires("LodgingType").HasValue("Person");
                }).Map<User>(m =>
                {
                    m.Requires("LodgingType").HasValue("User");
                });

    The result:

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by DaniloC Monday, September 2, 2013 8:43 AM
    Monday, September 2, 2013 7:21 AM
    Moderator

All replies

  • Hello DaniloC,

    Thank you for posting your question to this forum.

    From your description, you want to know why it throws an exception when you are trying to load the model.

    If I have misunderstood, please let me know.

    As far as I know, if we want to use Code-First TPH, we need to set the same column name for Persons and Users like codes below:

    modelBuilder.Entity<Person>().Map<Person>(m =>
                {
                    m.ToTable("Lodgings");
                    m.Requires("LodgingType").HasValue("Person");
                }).Map<User>(m =>
                {
                    m.Requires("LodgingType").HasValue("User");
                });

    The result:

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by DaniloC Monday, September 2, 2013 8:43 AM
    Monday, September 2, 2013 7:21 AM
    Moderator
  • I'm not sure this is what I'm looking for. Probably my question wasn't stated too clearly...

    I have a first level of inheritance Person => User, already implemented with TPT.

    public class Person
    {
        public int PersonId { get; set; }
        public string FullName { get; set; }
    }
    
    public class User : Person
    {
        public int PersonId { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
    }
    

    I now want a second level of inheritance on Users with TPH. I want for instance to have a CustomUser, which is gonna be very application specific. This is the class I want to create and fit in table "Users"

    public class CustomUser : User
    {
        public string CustomData { get; set; }
    }

    Adding the class makes the model fail to load with the above error.

    Please be patient with me, I'm really a noob in EF :P
    Thanks in advance,
    D.


    Monday, September 2, 2013 8:37 AM
  • My bad! Your answer appears to be the solution:) :) :)

    Thanks a lot

    Monday, September 2, 2013 8:43 AM