dbcontext

DBContext Find with Includes - where lambda with Primary keys

浪子不回头ぞ 提交于 2019-12-06 01:15:49
I am writing a generic repository to interface with EF using DBContext. I have a generic Get() method which receives a primary key value and returns the entity: public class DALRepository<DALEntity> : IDisposable, IGenericRepository<DALEntity> where DALEntity : class { private IDbSet<DALEntity> dbSet; private NWEntities context; public DALRepository() { context = new NWEntities(); context.Configuration.LazyLoadingEnabled = false; dbSet = context.Set<DALEntity>(); } Here's a simple get method - just works on the PK - exactly what I want. public DALEntity Get(string ID) { return dbSet.Find(ID);

Mocking DbEntityEntry

安稳与你 提交于 2019-12-06 00:17:57
I am writing unit tests for my Generic Repository layer but i have some problems with DbEntityEntry . My Update method is like below. public virtual void Update(TEntity entityToUpdate) { var entity = dbSet.Find(context.Entry<ITrackedEntity>(entityToUpdate).Entity.Id); context.Entry(entity).CurrentValues.SetValues(entityToUpdate); } As you can see, context.Entry<TEntity>(TEntity entity) method is invoked by caller. So while unit tests this code throws null exception. I had tried to mock context.Entry method but i couldn't because i couldn't provide return value. My current test method is below

How to use DbContext in T4 template?

和自甴很熟 提交于 2019-12-05 20:53:21
I want to generate some code with a T4 Template using EntityFramework. I created a T4 Template in the same Assembly as my currently working EF6 DbContext: <#@ template language="C#" hostspecific="true" debug="True" #> <#@ assembly name="$(SolutionDir)\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll" #> <#@ assembly name="$(TargetPath)" #> <#@ import namespace="Conwell.Administration.Data.Entities" #> <# using (var db = new KassenautomatEntities()) { #> //Hello World <# } #> When I run it, I get the following execption: Running transformation: System.InvalidOperationException: The

Tomcat: javax.naming.NoInitialContextException

耗尽温柔 提交于 2019-12-05 19:44:19
I have a webservice with Java Servlets on a Tomcat server. In my Servlet I use a database pool like this: envContext = new InitialContext(); DataSource ds = (DataSource) envContext.lookup( "java:/comp/env/jdbc/Database" ); con = ds.getConnection(); For initialisation I have this in my web.xml: <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/Database</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> Then there is the context.xml which seems to be the important step here: <?xml version="1.0" encoding="UTF-8"?> <!--

Generic Merge of DbContext: how to check if entity is all ready attached?

回眸只為那壹抹淺笑 提交于 2019-12-05 18:52:20
问题 Given the following code: void MergeDbContext(DbContext aSourceDbContext, DbContext aDestinationDbContext) { var sourceEntities = aSourceDbContext.ChangeTracker.Entries().ToList(); foreach (DbEntityEntry entry in sourceEntities) { object entity = entry.Entity; entry.State = EntityState.Detached; // check if entity is all ready in aDestinationDbContext if not attach bool isAttached = false;// TODO I don't know how to check if it is all ready attched. if (!isAttached) { aDestinationDbContext

Avoid Entity Framework Error with Multiple Tasks Running Concurrently on Same DbContext

こ雲淡風輕ζ 提交于 2019-12-05 18:02:38
问题 I have a WebApi controller in a Dotnet Core project running Entity Framework Core with Sqlite. This code in an action occationally produces errors: var t1 = _dbContext.Awesome.FirstOrDefaultAsync(a => [...]); var t2 = _dbContext.Bazinga.FirstOrDefaultAsync(b => [...]); var r1 = await t1; var r2 = await t2; The errors have been: Microsoft.EntityFrameworkCore.Query.RelationalQueryCompilationContextFactory:Error: An exception occurred in the database while iterating the results of a query.

Random errors occur with per-request DbContext

人走茶凉 提交于 2019-12-05 08:31:33
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 Command which must be closed first. - I turned on MARS to get rid off this (altough I believe it should

Entity Framework 6 DBContext with only a subset of all tables

强颜欢笑 提交于 2019-12-05 08:06:43
We have a huge database with 770 tables and want to do some performance testing with EF 6.1x. We want to query only 5 of those 770 tables. Is it possible to create a "light" DBContext with only 5-6 entities/DBSets instead of using the full 770-tables-context? When we use the full context, a simple query with 4 joins takes 45 seconds. Thats' 44 seconds too long. We are using code-first (reverse engineered). The problem: When we create such a "light" version of the full context (i.e. 5 tables only), EF complains that all the other entities that are somehow related to these 5 tables have missing

Does EF caches entities between different instances of DbContext?

99封情书 提交于 2019-12-05 05:24:39
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. Entity Framework doesn't have a data cache per AppDomain, only a cache per context instance. If you create a new context per request or query you start with an empty cache and EF will fetch

EF code first: inherited dbcontext creates two databases

不问归期 提交于 2019-12-05 04:44:38
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 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add