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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:33:35+00:00 2026-05-25T03:33:35+00:00

I use POCO in Entity Framework. Is any direct or indirect way in the

  • 0

I use POCO in Entity Framework. Is any direct or indirect way in the latest EF version to get Table name at the runtime to avoid hardcode values?

I need it inside my custom database initializer to run code like this:

context.Database.ExecuteSqlCommand(
    string.Format("DBCC CHECKIDENT ({0}, RESEED, {1})", tableName, newSeed))

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-05-25T03:33:36+00:00Added an answer on May 25, 2026 at 3:33 am

    I’m working from the assumption that your context looks something like mine, with each of the table names getting generated from the class names when you add a DbSet to your context. If that’s the case, you can achieve your goal with reflection, though it’s a little ugly:

    public class MyContext : DbContext
    {
        public MyContext() : base("MyDatabase")
        {
        }
    
        public DbSet<Video> Video { get; set; }
        public DbSet<VideoRating> Rating { get; set; }
        public DbSet<User> User { get; set; }
    
                protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    
        public class Initializer : IDatabaseInitializer<DashVaultContext>
        {
            public void InitializeDatabase(MyContext context)
            {
                if (!context.Database.Exists())
                {
                    context.Database.Create();
    
                    PropertyInfo[] propertyInfos = typeof(MyContext).GetProperties(BindingFlags.DeclaredOnly |
                    BindingFlags.Public | BindingFlags.Instance);
    
                    var newSeed = 1000; // Or whatever is appropriate
    
                    foreach (PropertyInfo propertyInfo in propertyInfos)
                    {
                        var tableName = propertyInfo.PropertyType.GetGenericArguments()[0].Name;
                        context.Database.ExecuteSqlCommand(
                            string.Format("DBCC CHECKIDENT ({0}, RESEED, {1})", tableName, newSeed));
                    }
                }
            }
        }
    }
    

    UPDATE: I removed the pluralization hack and just turned off pluralization in the generated table names (see the OnModelCreating override).

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

Sidebar

Related Questions

I'm trying to use LINQPad 4 (the latest beta) with Entity Framework POCO classes.
I have converted an Entity framework project to use POCO objects by removing the
I've tried to use Entity Framework 4 and POCO for my MVC 3 project.
I currently use the Entity Framework designer to generate my persistance objects and I
One of the much-anticipated features of Entity Framework 4 is the ability to use
I use Asp.net 4 in C# and Entity Framework 4. My web application is
I have an Entity Framework POCO class that is generated by a T4 Template.
In nearly all examples of POCO classes created for Entity Framework 4.1 collections are
Is it bad practice to use the generated objects from Entity Framework as business
I have a 'Customer' POCO entity within my Entity Framework 4 project. I want

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.