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 9004283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:51:56+00:00 2026-06-16T00:51:56+00:00

I would like to create a repository based on the entity type. Any ideas

  • 0

I would like to create a repository based on the entity type. Any ideas how to do that?

  • We have a lot of entities, so this needs to be generic code.
  • We need to have history tables for every entity that we have.
  • We are using code-first, DbContext, EF 5.0 using repository and unit of work patterns.

Below is the code we have to get changes in the SaveChanges method. For every modified and deleted entity we would like to save the old row into a history table. Below is hard coded for a single entity: ServiceLocation.

All entities have a corresponding history entity. For example, for the ServiceLocation entity there is a ServiceLocationHistory entity (and repository).

If the modifiedEntity is of type ServiceLocation, how do I create a ServiceLocationHistoryRepository? And make it generic for all types?

  public override int SaveChanges()
    {
        // Get the entities that changed
        var addedEntries = ChangeTracker.Entries().Where(e => e.State == EntityState.Added && e.Entity is IHistoryBase).Select(e => (TBaseEntity)e.Entity);
        var modifiedEntries = ChangeTracker.Entries().Where(e => e.State == EntityState.Modified && e.Entity is IHistoryBase).Select(e => (TBaseEntity)e.Entity);
        var deletedEntries = ChangeTracker.Entries().Where(e => e.State == EntityState.Deleted && e.Entity is IHistoryBase).Select(e => (TBaseEntity)e.Entity);

        // Debugging watch varibles
        int x = addedEntries.Count();
        int y = modifiedEntries.Count();
        int z = deletedEntries.Count();

        // Create the Type adapter
        TypeAdapter adapter = new TypeAdapter(new RegisterTypesMap[] { new HistoryRegisterTypesMap() });
        var serviceLocationHistoryRepository = new ServiceLocationHistoryRepository(this);

        foreach (var addedEntry in addedEntries)
        {
            addedEntry.FromDate = DateTime.Now;
            addedEntry.ToDate = DateTime.Parse("9999-12-31 00:00:00.0000000");
        }

        foreach (var modifiedEntry in modifiedEntries)
        {

            ServiceLocationHistory history = adapter.Adapt<ServiceLocation, ServiceLocationHistory>(modifiedEntry as ServiceLocation);
            serviceLocationHistoryRepository.Add(history);

            history.FromDate = modifiedEntry.FromDate;
            history.ToDate = modifiedEntry.FromDate = DateTime.Now;
            modifiedEntry.ToDate = DateTime.Parse("9999-12-31 00:00:00.0000000");
        }

        foreach (var deletedEntry in deletedEntries)
        {
            ServiceLocationHistory history = adapter.Adapt<ServiceLocation, ServiceLocationHistory>(deletedEntry as ServiceLocation);
            serviceLocationHistoryRepository.Add(history);

            history.FromDate = deletedEntry.FromDate;
            history.ToDate = DateTime.Now;
         }
        return base.SaveChanges();

    }
  • 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-16T00:51:56+00:00Added an answer on June 16, 2026 at 12:51 am

    Below is the proof of concept code I used to get the repository based on the type.

           private dynamic GetHistoryRepository(TBaseEntity entry)
        {
            // TODO - How well does the below perform?
            // TODO - Is there a way that we can only get one repository if there are multiple changed entities of the same type?
            // TODO - How can we not hard code the type name?   Put a property on each entity that gives us the entitys history repository?  Factory?
    
            // Get the History repository for the entity
            Type objType = Type.GetType("Data.MainBoundedContext.CALMModule.Repositories." + entry.GetType().Name.Split('_')[0] + "HistoryRepository");
            return  Activator.CreateInstance(objType, new object[] { this });
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a c++ type that mimic the build-in type exactly.
I have separate repository classes for each entity. Now I would like to implement
I have a subversion repository, and I would like to create a branch, but
i would like create a array of structure which have a dynamic array :
I would like to create this shape using just css. I am pretty sure
I would like to create a class that runs something (a runnable) at regular
I'm trying to create a generic LINQ-TO-SQL repository based on this post which basically
I have some third party plugins that are not available publicly. I would like
I would like to create a json object to send as a post array,
I would like to create set of strings and below is the only limitation.

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.