none
Entity Migration and inheritance mapping problem RRS feed

  • Question

  • Hi.

    I have some abstract class in my project and I'm using Entity Framework 5 with Migrations enabled. I tried to initialize my migration but I fond the following error:

    Conflicting configuration settings were specified for property 'Name' on type 'Users.User': 
    IsNullable = True conflicts with IsNullable = False

    The problem is that I have these classes:

        public abstract class User:EntityBase

        {
            public virtual string Name { getset; }
            public virtual string Email { getset; }
            public virtual string Password { getset; }
            public virtual bool IsActive { getset; }
        }
        public class UserCompany:User
        {
            public UserCompany()
            {
                IsActive = true;
            }
     
            public virtual Company Company { getset; }
        }

    and Player that is an User too :

       

    publicclassPlayer:User     {         public Player()         {             IsActive = true;         } }

    In my mapping I have:

     public UserCompanyMap()

            {
                ToTable("UserCompanies");
                Property(a => a.Name).HasMaxLength(200).IsRequired();
                Property(a => a.Email).HasMaxLength(300).IsRequired();       
            }

    And

        public PlayerMap()
            {
                ToTable("Players");
                Property(a => a.Name).HasMaxLength(200).IsOptional();
            }

    how you can see, my mapping has Name with max length 200(UserCompanyMap) and 300 for PlayerMap.

    I added an empty mapping of the abstract class, but the problem was not solved.

    Anyone can help me with this problem? Any suggestion?

    PS: The first time that I initialized my DB using migration, the code was created but I saw that some properties was not created.

    Thanks.

      

    Michel Magalhães

    Wednesday, December 19, 2012 2:22 AM

Answers

  • Your problem here is that when using TPT mapping the base class is its own table, and you cannot configure the same column in the database to have two different max lengths or be optional sometimes. There are a couple of ways I can think of to resolve this.

    1. Duplicate the name column in each derived type instead of putting it in the base class, this would give UserCompany and Player their own name column in the database that could have different restrictions.

    2. Use TPC instead of TPT, this would leave the classes as they are but the database would not have a table for User. Instead each property of User would have a column in the derived types tables. (See TPC example here: http://msdn.microsoft.com/en-us/data/jj591617#2.6).

    3. Do not set the restrictions in the database and instead enforce the max length in the domain, presumably set the database to have a max length of 300 and then enforce the smaller max length in your classes.

    Which of these you want to use depends on your application and the way you are structuring everything. Each of them should remove the problem, but I'm not sure which is best for your application.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by Alexander Sun Thursday, January 10, 2013 8:40 AM
    Monday, December 31, 2012 6:36 PM
    Moderator