Last post Aug 20, 2020 06:12 AM by yij sun
Aug 17, 2020 05:01 PM|ozgey99|LINK
We have a static DbContext that we use throughout our project. Here is the get method:
public static istanbulEntities DbContext
string ocKey = "ocm_" + HttpContext.Current.GetHashCode().ToString("x");
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:
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:
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?
Aug 18, 2020 07:05 AM|yij sun|LINK
Accroding to your description,could you post us more details about the DbContext implementation.
And your problem is clearly that an error occurred while updating the object context.What opeartions do you have do while updating database?
You could use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.
Aug 18, 2020 08:07 AM|ozgey99|LINK
Hello yij sun,
We use the same static DbContext object throughout the project. With this object, we perform all of the CRUD operations.
We are trying to replace this static object with dynamic instances that get disposed of once the request is handled.
We decided to do this gradually, so we replaced the static DbContext with local variable instances for the routine tasks that we have in Global.asax, however we were getting the errors mentioned above.
Aug 20, 2020 06:12 AM|yij sun|LINK
Accroding to your description,could you post your database and in the Entity Framework model to us?
As far as I think,your problem is you have some primary key .You could check the relation of your primary key,ForeignKey or ConditionalOptions.