The underlying provider failed on Open and other exceptions with local DbContext variables

守給你的承諾、 提交于 2020-08-20 10:33:19

问题


We have a static DbContext that we use throughout our project. Here is the get method:

public static istanbulEntities DbContext
{
    get
    {
        string ocKey = "ocm_" + HttpContext.Current.GetHashCode().ToString("x");
        if (!HttpContext.Current.Items.Contains(ocKey))
            HttpContext.Current.Items.Add(ocKey, new istanbulEntities());
        return HttpContext.Current.Items[ocKey] as istanbulEntities;
    }
}

We were having exceptions with some of the routine tasks that we have in Global.asax, and these problems were:

  • The property 'id' is part of the object's key information and cannot be modified.
  • The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: Saving or accepting changes failed because more than one entity of type 'TBL_MAIL' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.

We were getting these errors even though we never explicitly modified the primary key of any object. In order to solve this, we decided to use a local variable instead of the static DbContext, namely declaring it as:

var db = new istanbulEntities();

However, we tested all of the routine tasks by scheduling them at the same time, and we started getting more errors:

  • The underlying provider failed on Open.
  • The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.
  • The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: Saving or accepting changes failed because more than one entity of type 'TBL_MAIL_BOX' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.

We suspect that the first error causes the latter ones. There is no issue with the connection string, as it worked fine before. We are still not modifying any primary keys. We also think that there is an issue with concurrent inserts, but there aren’t many similar examples online, so we are not sure how to go about it.

Are these exceptions caused by attempting to open many connections to the database with the local DbContext variables while still keeping the static DbContext object?

来源:https://stackoverflow.com/questions/63455093/the-underlying-provider-failed-on-open-and-other-exceptions-with-local-dbcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!