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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:21:02+00:00 2026-05-17T23:21:02+00:00

Using a code-first approach I’d like to map a single model to multiple table

  • 0

Using a code-first approach I’d like to map a single model to multiple table names dynamically. Currently I can do something like modelBuilder.Entity(Of Person)().MapSingleType().ToTable("Managers") but as the OnModelCreating method is only called once I can’t map it to other table names on the fly.

In our current LinqToSql version we’re overriding the MetaModel.GetTable() and returning a new TableAttribute with our dynamic name. I haven’t found an attribute like that in EF (even if there were I wouldn’t know how to override that yet). So my question is: Is it possible to do this (yet)?

Update

I’ve found that I can prevent the OnModelCreating method from caching the mappings by calling

modelBuilder.CacheForContextType = false;

As a result I can assign table definitions on instantiation of the object. This isn’t quite how I wanted to do it but it works.

Update

Oh boy, was the above a big mistake…Caching exists for a reason! 🙂 So I’m back to square one with POCO object mapping. I’ll post an update if I figure out a solution.

Final

Incase anybody cares how I’ve currently solved this issue, here you go:

First I created a separate library with the POCO tables and an Interface

public interface IDataContext {
    System.Data.Entity.DbSet<TableGeneric> TableGeneric { get; set; }

    int SaveChanges();
}


public class TableGeneric {
    [Key]
    public int Column1 { get; set; }
    public string Column2 { get; set; }
    public DateTime Column3 { get; set; }
    public string Column4 { get; set; }
    public string Column5 { get; set; }
}

Then, using the CSharpCodeProvider I created a class that takes the following template and turns it into a type definition:

class DataContext : System.Data.Entity.DbContext, IDataContext {
    public System.Data.Entity.DbSet<TableGeneric> TableGeneric { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder
            .Entity<ContextTesting.Interfaces.EF.TableGeneric()
                .MapSingleType()
                .ToTable("$TableName$");
    }
}

With the generated type I’m able to create an instance so here we go

Type typeAccountants = BuildContext.CreateGenericTable("Accountants");
IDataContext context = (IDataContext)Activator.CreateInstance(typeAccountants);

Then the rest is just as if you had a normal DataContext. Hope this helps someone else.

  • 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-05-17T23:21:03+00:00Added an answer on May 17, 2026 at 11:21 pm

    Incase anybody cares how I’ve currently solved this issue, here you go:

    First I created a separate library with the POCO tables and an Interface

    public interface IDataContext {
        System.Data.Entity.DbSet<TableGeneric> TableGeneric { get; set; }
    
        int SaveChanges();
    }
    
    
    public class TableGeneric {
        [Key]
        public int Column1 { get; set; }
        public string Column2 { get; set; }
        public DateTime Column3 { get; set; }
        public string Column4 { get; set; }
        public string Column5 { get; set; }
    }
    

    Then, using the CSharpCodeProvider I created a class that takes the following template and turns it into a type definition:

    class DataContext : System.Data.Entity.DbContext, IDataContext {
        public System.Data.Entity.DbSet<TableGeneric> TableGeneric { get; set; }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder) {
            modelBuilder
                .Entity<ContextTesting.Interfaces.EF.TableGeneric()
                    .MapSingleType()
                    .ToTable("$TableName$");
        }
    }
    

    With the generated type I’m able to create an instance so here we go

    Type typeAccountants = BuildContext.CreateGenericTable("Accountants");
    IDataContext context = (IDataContext)Activator.CreateInstance(typeAccountants);
    

    Then the rest is just as if you had a normal DataContext. Hope this helps someone else.

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

Sidebar

Related Questions

No related questions found

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.