title: Registering Entity Mapping
body: <p>I am working in <strong>asp.net core</strong> project using <strong>EF core</strong>. I mapped my entities by overriding the <strong>OnModelCreating</strong> function in the context class. I can easily map these entities manually. Well, I would better post my codes and explain .. </p>
  
  <p>This is what I did in my Context class:</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder builder)
      {        
          base.OnModelCreating(builder);
          //builder.RegisterEntityMapping&lt;CodeTable, CodeTableMap&gt;();
          builder.RegisterEntityMapping&lt;Country, CountryMap&gt;();
          builder.RegisterEntityMapping&lt;State, StateMap&gt;();
          builder.RegisterEntityMapping&lt;City, CityMap&gt;();
          builder.RegisterEntityMapping&lt;User, UserMap&gt;();
          builder.RegisterEntityMapping&lt;Prospect, ProspectMap&gt;();
      }
  </code></pre>
  
  <p><strong>Country.cs</strong></p>
  
  <pre><code>public class Country:BaseEntity
  {
  
      public string CountryCode { get; set; }
      public string CountryName { get; set; }
      public string Currency { get; set; }
      public string CurrencyName { get; set; }
      public string UnitOfMeasure { get; set; }
      public string TelephoneCountryCode { get; set; }
      public string CurrencySymbol { get; set; }
  
      public virtual ICollection&lt;State&gt; States { get; set; }
      public virtual ICollection&lt;City&gt; Cities { get; set; }
  }
  </code></pre>
  
  <p><strong>CountryMap.cs</strong></p>
  
  <pre><code>public class CountryMap : QuantumEntityTypeConfiguration&lt;Core.Domain.Country&gt;
  {
      public override void Map(EntityTypeBuilder&lt;Core.Domain.Country&gt; builder)
      {
          builder.ToTable("Country");
          builder.HasKey(pr =&gt; pr.CountryCode);
  
          builder.HasMany(m =&gt; m.Cities).WithOne(i=&gt; i.Country).HasForeignKey(m=&gt; m.CountryCode);
          builder.HasMany(m =&gt; m.States).WithOne(i =&gt; i.Country).HasForeignKey(m =&gt; m.CountryCode);
      }
  }
  </code></pre>
  
  <p>But, I would like to do it dynamically as it would be hectic later on to map all the models. I could find the solution in nopCommerce's context class where they did in this way: </p>
  
  <pre><code> protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          //dynamically load all configuration
          //System.Type configType = typeof(LanguageMap);   //any of your configuration classes here
          //var typesToRegister = Assembly.GetAssembly(configType).GetTypes()
  
          var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
          .Where(type =&gt; !String.IsNullOrEmpty(type.Namespace))
          .Where(type =&gt; type.BaseType != null &amp;&amp; type.BaseType.IsGenericType &amp;&amp;
              type.BaseType.GetGenericTypeDefinition() == typeof(NopEntityTypeConfiguration&lt;&gt;));
          foreach (var type in typesToRegister)
          {
              dynamic configurationInstance = Activator.CreateInstance(type);
              modelBuilder.Configurations.Add(configurationInstance);
          }
          //...or do it manually below. For example,
          //modelBuilder.Configurations.Add(new LanguageMap());
  
  
  
          base.OnModelCreating(modelBuilder);
      }
  </code></pre>
  
  <p>I tried to implement that and I got an error: </p>
  
  <pre><code> protected override void OnModelCreating(ModelBuilder builder)
      {
          // TODO: Use Dynamic mapping by getting classes which uses QuantumEntityTypeConfiguration
          var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
          .Where(type =&gt; !String.IsNullOrEmpty(type.Namespace))
          .Where(type =&gt; type.BaseType != null &amp;&amp; type.BaseType.IsGenericType &amp;&amp;
              type.BaseType.GetGenericTypeDefinition() == typeof(QuantumEntityTypeConfiguration&lt;&gt;));
          foreach (var type in typesToRegister)
          {
              dynamic configurationInstance = Activator.CreateInstance(type);                   
              builder.Configurations.Add(configurationInstance); //Error in this line specifying:ModelBuilder has no defination for Configuration.
          }}
  </code></pre>
  
  <p><strong>Error</strong>
  <a href="https://i.stack.imgur.com/oZkV0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oZkV0.png" alt="enter image description here"></a></p>
  
  <p>Well nopCommerce uses the <strong>DbModelBuilder</strong> from namespace: <strong>System.Data.Entity</strong> and I use <strong>ModelBuilder</strong> under namespace : <strong>Microsoft.EntityFrameworkCore</strong>. </p>
  
  <p>So, if you have any solutions or recommendations. please let me know.</p>