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

  • Home
  • SEARCH
  • 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 8149199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:48:06+00:00 2026-06-06T14:48:06+00:00

I have to write a C# application that works with a SQL server database

  • 0

I have to write a C# application that works with a SQL server database created and mantained by an old application. The application creates new tables each year and the “year property” is in the table name. The number of tables it creates may vary depending of the number of “sections” that the user has created inside the application. So, I have to work with tables like Cwx_DRyz (quite self explanatory…), where “wx” can be the section, and “yz” would be the year. An example of group of table could be:

C01_DR07

C01_DR08

C01_DR09

C02_DR08

C02_DR09

C03_DR06

C04_DR12

And all of those tables could represent, for example, clients. They would be clients from different sections and years, but clients with the same structure.

My question is: Can I have a Client entity to handle all those tables and change the mapping from one to another at runtime? The title says “unknown” because I don’t know the tables before runtime.

The most similar question I have found is Entity Framework map multiple tables to one entity and the answer is to use the “Table Per Concrete Type Inheritance”, but it is not useful for my case.

PS: EF version 4.3.1 and VS2010

EDIT: The tables don’t have primary keys… Most of them have columns that are supossed to have unique values (integer or string).

  • 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-06T14:48:07+00:00Added an answer on June 6, 2026 at 2:48 pm

    If you use “code first” you could create a mapping as you want. This also works with existing databases when the mapping you have created match the database.

    So whenever you create a context you can build the string (tablename) you want to map to.

    Some codesamples for “code first” and how you could start:

    The DbContext:

    public DbSet<YourEntity> YourEntities { get; set; }
    ...
    
    // this is called when the db gets created and does the configuration for you => maybe not needed in your case
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        ConfigurationRegistrar configurationRegistrar = modelBuilder.Configurations;
    
        new GeneralEntitiesConfiguration(configurationRegistrar);
    }
    

    GeneralEntitiesConfiguration is a class im using to handle the configurations, nothing more than a helper which looks like:

    public class GeneralEntitiesConfiguration
    {
        public GeneralEntitiesConfiguration(ConfigurationRegistrar configurationRegistrar)
        {
            configurationRegistrar.Add(new YourEntityConfiguration());
            //and additional configurations for each entity, just to splitt it a bit and have it more read and maintenance able
        }
    }
    

    YourEntityConfiguration is a class where i have all the configurations for this entity:

    public class YourEntityConfiguration : EntityTypeConfiguration<YourEntity>
    {
        public YourEntityConfiguration ()
        {
            ToTable("WhatEverYouLike"); // here you can do any magic to map this entity to a table, just make sure that your properties are mapped to the correct colums
            Property(entity => entity.Id).HasColumnName("YouColumnName");
    
            //and here you also have to do the other configurations
        }
    }
    

    At the application startup (or before you initialize your context the first time) you have to initialize the database. Therefore you can use an initializer which checks the database and handles differences. Build in there are things like “DropCreateDatabaseAlways” or “DropCreateDatabaseIfModelChanges” => you would need to create your own which just ignores any differences. In my sample i have create one which just throws an exception when the model differs (i wanted to handle model changes with scipts for the first try):

    //before using the context the first time i'm calling, you can ignore the connection string
    DbContextInitializer.Init(conString);
    
    public static class DbContextInitializer
    {
        public static void Init (string connectionString)
        {
            Database.SetInitializer(new CreateDbThrowExceptionIfModelDiffersInitializer<SMDbContext>());
    
            using(var dbContenxt = new MyDbContext(connectionString))
            {
                try
                {
                    dbContenxt.Database.Initialize(true);
                }
                catch(DatabaseModelDiffersException diffException)
                {
                    // some magic...
                }
                catch(Exception ex)
                {
                    // TODO: log
                    throw;
                }
            }
        }
    
        public class CreateDbThrowExceptionIfModelDiffersInitializer<TContext> : IDatabaseInitializer<TContext> where TContext : DbContext
        {
            public void InitializeDatabase(TContext context)
            {
                using (new TransactionScope(TransactionScopeOption.Suppress))
                {
                    if (!context.Database.Exists())
                        context.Database.Create();
                }
    
                if (!context.Database.CompatibleWithModel(true))
                {
                    throw new DatabaseModelDiffersException("Database Model differs!");
                }
            }
    
            protected virtual void Seed(TContext context)
            {
                // create data if you like
            }
        }
    
        // just an exception i'm using for later useage
        public class DatabaseModelDiffersException : Exception
        {
            public DatabaseModelDiffersException(string msg) : base(msg)
            {}
        }
    }
    

    Hope you have got an idea of you can handle dynamic table names with entity framework!
    If there are more questions just ask 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a desktop application that uses an SQL Server CE 3.5 database. I
I have a web application that loads some 50 tables from SQL Server into
I have a SQL Server 2008 database with a table that contains a FILESTREAM
I have a java application that sends text to a sql database on a
We have an ASP.NET application that uses SQL Server to store its session state.
I am working on a .NET web application that uses an SQL Server database
I have to write an application that implements a secure connection between client and
I have been asked to write an application that is going to act as
I have an application that write data in the programdata folder in vista. The
I have to write an application where I retrieve a lotto numbers that I

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.