EF4.3 beta1 "Map was called more than once for type 'ModelName' and at least one of the calls didn't specify the target table name"

Proposed Answer EF4.3 beta1 "Map was called more than once for type 'ModelName' and at least one of the calls didn't specify the target table name"

  • Thursday, February 02, 2012 10:04 AM
     
     

    this below was working with EF 4.2 but getting an error with EF 4.3 Beta1 for a Table Per Hierarchy setup

     internal class ParentmodelConfiguration : BaseEntityConfiguration< Parentmodel>

        {

            public  ParentmodelConfiguration  ()

            {

                this.Map<Childmodel>(m => m.Requires("RequiredFlag").HasValue(false).IsRequired());

                this.Map< Childmodel2>(m => m.Requires(" RequiredFlag").HasValue(true).IsRequired());           

            }

     

     public class   Childmodel : Parentmodel

        {

        }

     public class   Childmodel2 : Parentmodel

        {

        }

      public abstract class  Parentmodel 

        {

All Replies

  • Friday, February 10, 2012 3:46 PM
     
     Proposed Answer
    I fixed this by creating a configuration class for each child, and putting the type discrimination mapping for each child in the child configuration.
    • Proposed As Answer by Matt J C Saturday, February 11, 2012 1:58 AM
    •  
  • Saturday, February 11, 2012 1:58 AM
     
      Has Code

    Thanks Antoine for the tip, I just upgraded a project and this had me stumped.  Although this has to be a bug (don't you think?). But at least we can make it work for now. 

    For the example above, something like this:

         internal class ParentmodelConfiguration : BaseEntityConfiguration
        {
            public ParentmodelConfiguration()
            {          
                // put your mappings here (properties, table, key)
            }
        }
        internal class ParentmodelConfiguration<TEntityType> : BaseEntityConfiguration where TEntityType : class
        {
            internal ParentmodelConfiguration(ParentmodelType parentModelType)
            {
                // would recommend an "int" discriminator            
                // int discriminatorValue = (int)parentMdoelType;            
                // but for a bool, you could have this
                bool discriminatorValue = parentModelType.Childmodel ? false: true;
                this.Map(m => m.Requires("RequiredFlag").HasValue(discriminatorValue));
            }
        }
    
        public enum ParentmodelType
        {
            Childmodel,
            Childmodel2
        }
    
        // call it with
        modelBuilder.Configurations.Add(new ParentmodelConfiguration());
        modelBuilder.Configurations.Add(new ParentmodelConfiguration(ParentmodelType.Childmodel));
        modelBuilder.Configurations.Add(new ParentmodelConfiguration(ParentmodelType.Childmodel2));

    • Edited by Matt J C Saturday, February 11, 2012 2:18 AM
    • Edited by Matt J C Saturday, February 11, 2012 2:20 AM
    • Edited by Matt J C Saturday, February 11, 2012 2:22 AM
    •