dbcontext

Random errors occur with per-request DbContext

点点圈 提交于 2019-12-07 06:44:49
问题 I'm experiencing random errors (several per day) in my mvc+ef+unity application under higher load (10+ request per sec): The connection was not closed / The connection's current state is connecting deadlocks on Count queries (no explicit transaction) An item with the same key has already been added. in System.Data.Entity.DbContext.SetTEntity while resolving DbContext The remote host closed the connection. The error code is 0x80070057 There is already an open DataReader associated with this

How should I scope dependency injection of Entity Framework DbContext in a web app? (InstancePerHttpRequest vs SingleInstance)

岁酱吖の 提交于 2019-12-07 04:35:22
问题 I have read that DbContext object should be created as InstancePerHttpRequest, not SingleInstance, because of its thread-unsafe nature and it might consume too much resource between requets which makes sence. But I am using Repository objects which uses DbContext instance. Should I make them InstancePerHttpRequest or make them SingleInstance and use DependencyResolver to get the current DbContext. What would the best object creation design be, for Autofac (or any other DI), DbContext,

Get DbContext for Entities

放肆的年华 提交于 2019-12-07 02:33:25
Okay, I feel a bit foolish for having to ask this but I guess my understanding of the inner workings of Entity Framework is lacking. I'd like to experiment with work with DbContext . I have an existing ASP.NET MVC application using EF 4.2. I can get my entities using: var context = new MyEntities(); And this works just fine. But how the heck to I get the same data represented by a DbContext ? So I guess you are using default code generator provided by EDMX designer - it will use ObjectContext and heavy weight EntityObject based entities. If you want to use DbContext you must: Turn off that

Using IQueryable<TEntity> instead DbSet<TEntity> problem

 ̄綄美尐妖づ 提交于 2019-12-07 02:22:38
问题 i stumbled to the next problem... I have database context: // For support unit testing... public interface IDbContext : IDisposable { IQueryable<Hardware> Hardwares { get; } IQueryable<ProviderHardware> ProviderHardwares { get; } } // Real DbContext (EF 4.0, Code First) public class PrimaryDbContext : DbContext, IDbContext { public DbSet<Hardware> Hardwares { get; set; } public DbSet<ProviderHardware> ProviderHardwares { get; set; } IQueryable<Hardware> IDbContext.Hardwares { get { return

EF code first: inherited dbcontext creates two databases

主宰稳场 提交于 2019-12-07 01:24:49
问题 I'm trying to create a base dbcontext that contains all the common entities that will always be reused in multiple projects, like pages, users, roles, navigation etc. In doing so I have a ContextBase class that inherits DbContext and defines all the DbSets that I want. Then I have a Context class that inherits ContextBase where I define project specific DbSets. The classes are defined as follows: public class ContextBase : DbContext { public virtual DbSet<User> Users { get; set; } //more sets

Does EF caches entities between different instances of DbContext?

别来无恙 提交于 2019-12-07 01:04:18
问题 Does creating DbContext per query in Asp.net make EF only read the data from its cache, or does it query DB for the whole sets every time? I know about metadata caching per AppDomain, but what about just the data? Context: data acquisition and visualisation application with MVC4 + Web API frontend, wouldn't call that "high volume", but lots of queries return the same sets of data in some shorter time frame. 回答1: Entity Framework doesn't have a data cache per AppDomain, only a cache per

Where to subscribe to ObjectMaterialized using EF6?

核能气质少年 提交于 2019-12-07 00:46:45
问题 I'm trying to subscribe my context to the OnjectMaterialized event following this, like so: ((IObjectContextAdapter)this).ObjectContext .ObjectMaterialized += ObjectContext_OnObjectMaterialized; But I'm using EF6 and the OnContextCreated method mentioned on that post does not exists in this version. I tried subscribing the materialized event at the context constructor, but then, if the database is deleted (which we do often during integration tests), the event is no longer subscribed. We

DbContext has been disposed

笑着哭i 提交于 2019-12-07 00:32:42
问题 I developed a web application with ASP.NET MVC 4 and SQL Server 2008, I create ContextManager class to have only one database context in all pages. public static class ContextManager { public static HotelContext Current { get { var key = "Hotel_" + HttpContext.Current.GetHashCode().ToString("x") + Thread.CurrentContext.ContextID.ToString(); var context = HttpContext.Current.Items[key] as HotelContext; if (context == null) { context = new HotelContext(); HttpContext.Current.Items[key] =

Exception has been thrown by the target of an invocation thrown when scaffolding a controller

旧时模样 提交于 2019-12-06 20:20:18
问题 I created a separate Class Library project to store the Database Context and Model Classes. In the same solution, I created an ASP.NET MVC project and referenced the Class Library project, as well as include the Connection String for the Database Context, in the project's Web.config file. However, when I attempt to add a Controller (with views, using EF), I get the following error: Exception has been thrown by the target of an invocation. I am able to see the Database Context and Model

Entity Framework 4.2 - How to realize TPT-Inheritance with Database-generated Primarykey Value?

╄→гoц情女王★ 提交于 2019-12-06 15:19:45
I want to use the EF (4.2) in the following scenario: There exists a database already (so I chose the database-first approach) and it is a SQL Anywhere DB. I want to use persistence-ignorant business objects, so I use the DbContext Template to generate POCO classes from the EDM. There is one simple inheritance hierarchy among my entities: an abstract base entity and two concrete derived entities. In the database there is one table for each type of the inheritance hierarchy (Table-Per-Type Strategy). Each of these three tables has a primary key column ( Id , type: integer ), and the association