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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:18:20+00:00 2026-06-11T19:18:20+00:00

I’m having a strange concurrency issue with some code I wrote. First thing you

  • 0

I’m having a strange concurrency issue with some code I wrote. First thing you will ask is why am I using CF and using DbModelBuilder… well I have situation where I could have Nth number of databases that will all be identical and I must maintain seperation of data. I’m using CF to prevent a massive model generation issues with multiple devs foobaring my models with their GUI’s. Alas, this is the basic code for what I’m doing.

The two console writelines should never be hit and yet they are.

Can you please try this and let me know where the issue is?

This is the Test Harness console app:

class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i < 100; i++)
        {
            var workerObject = new Worker();
            var workerThread = new Thread(workerObject.DoWork);
            workerThread.Start();
        }
    }
}

public class Worker
{
    public void DoWork()
    {
        var connections = new List<KeyValuePair<string, DynamicDb>>
            {
                new KeyValuePair<string, DynamicDb>("db1", new MyDynamicEntityLayer("db1").DynamicDb()),
                new KeyValuePair<string, DynamicDb>("db2", new MyDynamicEntityLayer("db2").DynamicDb())
            };

        foreach (var db in connections)
        {
            if (db.Key == "db1" && db.Value.ConnectionString.Contains("db2"))
                Console.WriteLine("THIS SHOULD NEVER HAPPEN!!! db1 : " + db.Value.ConnectionString);
            if (db.Key == "db2" && db.Value.ConnectionString.Contains("db1"))
                Console.WriteLine("THIS SHOULD NEVER HAPPEN!!! db2 : " + db.Value.ConnectionString);
        }
    }
}

Here is the entity class:

public class MyDynamicEntityLayer
{
    public static string ConnectionString { get; set; }
    public MyDynamicEntityLayer(string db = null)
    {
        MakeAConnectionString(db);
        Database.SetInitializer<DynamicDb>(null);
    }

    public void MakeAConnectionString(string db)
    {
        var sqlBuilder = new SqlConnectionStringBuilder();
        if (db == "db1")
        {
            sqlBuilder.DataSource = "MySqlServer";
            sqlBuilder.InitialCatalog = "db1";
            sqlBuilder.ConnectTimeout = 180;
            sqlBuilder.IntegratedSecurity = true;
        }
        else
        {
            sqlBuilder.DataSource = "MySqlServer";
            sqlBuilder.InitialCatalog = "db2";
            sqlBuilder.ConnectTimeout = 180;
            sqlBuilder.IntegratedSecurity = true;
        }

        ConnectionString = sqlBuilder.ToString();
    }

    public DynamicDb DynamicDb()
    {
        var builder = new DbModelBuilder(DbModelBuilderVersion.Latest);
        TableMappingToBuilder(builder);
        var compiled = builder.Build(Database.DefaultConnectionFactory.CreateConnection(ConnectionString)).Compile();
        return new DynamicDb(compiled);
    }

    public void TableMappingToBuilder(DbModelBuilder builder)
    {
        builder.Configurations.Add(new EntityTypeConfiguration<ConcurrencyTest>());
        builder.Entity<ConcurrencyTest>().ToTable("ConcurrencyTest", "dbo");
    }
}

and my DbContext CS file:

public class DynamicDb : DbContext
{
    public string ConnectionString { get; set; }
    public DynamicDb(DbCompiledModel model)
        : base(MyDynamicEntityLayer.ConnectionString, model)
    {
        ConnectionString = MyDynamicEntityLayer.ConnectionString;
        ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 100;
    }
    public DbSet<ConcurrencyTest> ConcurrencyTests { get; set; }
}

public class ConcurrencyTest
{
    public int ID { get; set; }
    public string MyColumn { get; set; }
}
  • 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-11T19:18:21+00:00Added an answer on June 11, 2026 at 7:18 pm

    Your issue is:

    public class MyDynamicEntityLayer
    {
        public static string ConnectionString { get; set; } // STATIC PROPERTY
        ...
    
    }
    

    All worker threads share single value stored in this property and fights for its modification.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.