Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:50:28+00:00 2026-06-13T20:50:28+00:00

I’ve been trying to implement the Repository pattern using EntityFramework5. I already got the

  • 0

I’ve been trying to implement the Repository pattern using EntityFramework5. I already got the basics to work. My repositories are being loaded only when necessary and the data is being inserted/updated/retrieved in the database as it should.
The problem is: I can’t get the tables with mapping to be correctly created, or even created at all.
I’ve been reading some articles online, even here, talking about ways to work with mapped tables and create them in the database, but I haven’t succeed. Hands my question here.

How do I get it to work in my scenario?

I’ll explain what I’m doing so far:

This method below is overriden to, supposedly, create the mappings and tables.

PS: Generics.InvokeGenericMethod is a method I created and it’s working. It does what it says 🙂

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        foreach (Type entityType in Repositories.Keys)
        {
            //Invokes the Entity method from DbModelBuilder injecting the class type.
            Generics.InvokeGenericMethod(modelBuilder, typeof(DbModelBuilder), entityType, "Entity", null);
        }

        foreach (GaiaEntityConfiguration config in EntityConfigurations)
        {
            //Defines each mapping existing in the context
            config.SetConfiguration(modelBuilder);
        }
    }

The part that is not working is where I use SetConfiguration. SetConfiguration is a method I created for each class added to the context that has mapping, have that mapping mirrored to the database.

Here’s one example (in this case a recipient has many messages and messages have many recipients / many-to-many association):

PS: I left the commented code so you can see one other approach I tried.

    public RecipientEntityConfiguration()
    {
        this.LeftKey = "RecipientId";
        this.RightKey = "MessageId";
        this.Schema = "Hermes";
        this.TableName = "RecipientMessage";
    }

    public override void SetConfiguration(DbModelBuilder modelBuilder)
    {
        //EntityTypeConfiguration<Recipient> entityTypeConfiguration = new EntityTypeConfiguration<Recipient>();

        //entityTypeConfiguration
        //    .HasMany<Message>(r => r.Messages)
        //    .WithMany(m => m.Recipients)
        //    .Map(mr =>
        //    {
        //        mr.MapLeftKey(this.LeftKey);
        //        mr.MapRightKey(this.RightKey);
        //        mr.ToTable(this.TableName, this.Schema);
        //    });
        modelBuilder.Entity<Recipient>()
            .HasMany<Message>(r => r.Messages)
            .WithMany(m => m.Recipients)
            .Map(mr =>
            {
                mr.MapLeftKey(this.LeftKey);
                mr.MapRightKey(this.RightKey);
                mr.ToTable(this.TableName, this.Schema);
            });
        //modelBuilder.Configurations.Add<Recipient>(entityTypeConfiguration);
    }

When this.Database.Initialize(false); is called I get this error:

Value cannot be null.
Parameter name: key

StackTrace:

   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at System.Data.Entity.ModelConfiguration.Configuration.Mapping.SortedEntityTypeIndex.Add(EdmEntitySet entitySet, EdmEntityType entityType)
   at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingService.Analyze()
   at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingService.Configure()
   at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
   at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Database.Initialize(Boolean force)
   at Gaia.Repository.GaiaContext..ctor(GaiaContextConfiguration config) in E:\Gaia\Gaia.Repository\GaiaContext.cs:line 37
   at Hermes.HMail.SendMessage(Int64 sender, Int64[] toUsers, String subject, String content, FileStream attachment) in G:\Gaia\Hermes\HMail.cs:line 78
   at Gaia.Controllers.ApplicationController.Test() in G:\Gaia\Gaia\Controllers\ApplicationController.cs:line 18
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.<>c__DisplayClass1.<WrapVoidAction>b__0(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

Any ideas?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:50:30+00:00Added an answer on June 13, 2026 at 8:50 pm

    First of all, I figured out in EF Book pg.57, that any in-line configurations, should appears only after all configurations added to modelbuilder. Checks your code.

    Next, try this version of code, for automatiс adding mapping to modelbuilder.
    The goal is to remove calls from EntityTypeConfigurations and use pure method of adding configurations to context. Move your code from SetConfiguration to constructor and try to use my version. It’s worked for me as well.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         var entityMapTypes = assembly.GetTypes().Where(
             (t => t.BaseType != null && t.BaseType.IsGenericType && 
                   t.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)));
    
         foreach (var mapType in entityMapTypes)
         {
             dynamic configurationInstance = Activator.CreateInstance(mapType);
             modelBuilder.Configurations.Add(configurationInstance);
         }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.