dbcontext

Entity Framework 5 - Extending DBContext Class

匆匆过客 提交于 2019-12-24 03:07:39
问题 I don't know if I'm doing something wrong here or not... As a bit of background, I am working on an Entity Framework (v5.0) solution and was looking to add extra functionality to the DBContext class, so any tt-generated classes (that inherit from DbContext) will have that inherent functionality available to them automatically. Based upon the answer I saw here, I figured it would be as easy as simply adding in a new class that would look as follows: Imports System.Data.Entity Imports System

How to pass connection string from appsettings.[name].json to DbContext in .NET Core?

痴心易碎 提交于 2019-12-24 01:46:19
问题 I have a .net core app. I write my connection string in appsettings.json file. But, now, I want to get that connection string for my context. How to do that? In .net framework 4.6 I used this: ConfigurationManager.ConnectionStrings["ConnStrName"].ConnectionString; I have appsettings.Development.json like this (*** could be any name): "ConnectionStrings": { "***": "Server=***;Database=****;Trusted_Connection=True;MultipleActiveResultSets=true;" } I added service: services.AddTransient

Using Test Doubles with DbEntityEntry and DbPropertyEntry

落花浮王杯 提交于 2019-12-23 19:23:16
问题 I am using the new Test Doubles in EF6 as outlined here from MSDN . VS2013 with Moq & nUnit. All was good until I had to do something like this: var myFoo = context.Foos.Find(id); and then: myFoo.Name = "Bar"; and then : context.Entry(myFoo).Property("Name").IsModified = true; At this point is where I get an error: Additional information: Member 'IsModified' cannot be called for property 'Name' because the entity of type 'Foo' does not exist in the context. To add an entity to the context

How to use DBContext.Add/Attach (using EF CodeFirst 4.1) with nested opbjects

懵懂的女人 提交于 2019-12-23 12:18:24
问题 Problem: When adding an object "Order" to my dbcontext, all nested objects of the order gets "readded" to the database, though the nested objects is static data and only a reference shoudl be added in the database. Example: The database holds 0 orders, and 3 items. I add one order with 2 items. Now the database hold 1 order, and 5 items. The two items in the order has been "readded" to the database, even though the items had the right primary keys before db.SaveChanges(). I realize that i may

Updating DbSet<T> item's value from the controller - C#, MVC, Code First

二次信任 提交于 2019-12-23 03:51:59
问题 here's my question: I have an MVC3 C# application and I need to update a value in a DbSet from the Edit controller, so the T item's value will be replaced with a new one. I don't want to delete the item and add it again. I cannot figure out how to do this. DbSet doesn't seem to have something like an index. Here's my Edit controller: public class ItemController : Controller { private SESOContext db = new SESOContext(); private FindQrs fqr = new FindQrs(); [HttpPost] public ActionResult Edit

Entity Framework: multiple DB having the same schema

与世无争的帅哥 提交于 2019-12-23 03:26:18
问题 I have just created an ASP.NET MVC 4 & WebAPI project. After that I have added .edmx data source to project. I have multiple databases with the same schema. Dynamically I want to replace connection string using default constructor provided in EF. But in Model1.Designer.cs , every time I get error like "Member with same signature already declared". I'm unable to solve this problem. 回答1: Yes, it works! All you need to change is the connection string . And I have just tested it in order to

Creating DbContext with both custom ConnectionString and DbProviderInfo

此生再无相见时 提交于 2019-12-23 01:47:27
问题 We have a plethora of databases all sharing the same schema. They are spread about on a number of SQL Servers, some 2005, some 2008. We are getting an exception when connecting to a 2005 server AFTER having connected to a 2008 server first stating the following: An error occurred while updating the entries. See the inner exception for details. An error occurred while updating the entries. See the inner exception for details. The version of SQL Server in use does not support datatype

Dependency injection with EF DbContext that implements 2 interfaces

白昼怎懂夜的黑 提交于 2019-12-22 18:24:03
问题 Given a DbContext that implements 2 interfaces like so: public interface IQueryEntities { IQueryable<User> Users { get; } IQueryable<Computer> Computers { get; } // other IQueryable<T> get properties } public interface IUnitOfWork { int SaveChanges(); } public class MyContext : DbContext, IQueryEntities, IUnitOfWork { // implement interfaces using EF } First question, is it a bad idea to separate out the query aspects of DbContext (IDbSets) from the command aspects (SaveChanges)? I am

Mocking DbEntityEntry

删除回忆录丶 提交于 2019-12-22 10:50:04
问题 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

Entity Framework 6 DBContext with only a subset of all tables

核能气质少年 提交于 2019-12-22 05:48:07
问题 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