Questions tagged [dbcontext]

The DbContext API first shipped with Entity Framework version 4.1 and provides a more productive surface for working with the Entity Framework and can be used with the Code First, Database First, and Model First approaches.

The DbContext class represents a combination of the Unit-Of-Work and Repository patterns and enables you to query a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.

Links:

2528 questions
11
votes
2 answers

DbContextOptionsBuilder does not contain a definition for 'UseSqlite'

Dotnet Core Web API throws when trying to use SQLite for my application DbContextOptionsBuilder' does not contain a definition for 'UseSqlite' and no accessible extension method 'UseSqlite' how to fix this? I have tried…
Dev Ethio
  • 113
  • 1
  • 4
11
votes
1 answer

EF Core / DbContext > Map custom type as primary key

Using the fluent api, how do I map a custom type as the primary key within my OnModelCreating method of the DbContext class? Using EF Core I'm trying to build a model for the follow entity. public class Account { public AccountId AccountId {…
Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
11
votes
1 answer

Type or namespace name Mock<> could not be found Entity Framework 6

I am trying to mock my DbContext for writing my unit tests. I saw a tutorial, and tried to do it like the following: [TestMethod] public void MyFirstTest() { var mockSet = new Mock>(); } But I am getting the following…
Dan The Man
  • 1,835
  • 6
  • 30
  • 50
11
votes
1 answer

Refreshing Sql Connection Azure AD access token inside long-lived Entity Framework Context

I'm trying to set up a few .NET applications to use certificate-based authentication to Azure Active Directory and then use Active Directory to authorize my access to a Sql Azure DB. The problem I'm running into is that some parts of the application…
Nick
  • 695
  • 8
  • 23
11
votes
1 answer

Entity Framework Core DbContext and Dependency Injection

I'm building a service application using Web API, .Net Core and EntityFramework Core. For configuring options in my DbContext I'm using these lines in "ConfigureServices" method in Startup.cs var connection =…
11
votes
1 answer

EF5 db.Database.SqlQuery mapping returned objects

I have two C# classes public class SearchResult { public int? EntityId { get; set; } public string Name { get; set; } public Address RegisteredAddress { get; set; } } and public class Address { public int? AddressId { get; set; } …
user1714591
  • 283
  • 2
  • 11
11
votes
1 answer

Get DBContext from ObjectContext

I'm using the latest versions of ASP.NET MVC, SQL Server, and Entity Framework. I am not using code first. I can easily create an object context using the class generated by my EDMX file. But is there any way to obtain a DBContext object from that…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
10
votes
3 answers

Entity Framework (4.2) HasRequired results in unexpected LEFT OUTER JOIN

It appears that the Entity Framework (latest version from NuGet) may be ignoring the HasRequired configuration when constructing joins for Navigation Properties other than the first one defined. For example, given a POCO object (Person) with the…
mindlessgoods
  • 1,135
  • 1
  • 11
  • 22
10
votes
2 answers

How to properly configure the `services.AddDbContext` of `ConfigureServices` method

I'm trying to run an .NET Core Web application with EF Core. In order to test the repository I've added an MyDbContext that inherits the EF DbContext and interface IMyDbContext. public interface IMyDbContext { DbSet Models { get; set;…
Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91
10
votes
1 answer

EF Code First Connection String during Runtime with Impersonated Windows Service Account

I am required to create separate windows service accounts for each environment (dev, acceptance, and production) that my desktop application uses to connect to one of our internal databases. A global group has been added to these accounts to…
bjhuffine
  • 924
  • 1
  • 11
  • 23
10
votes
2 answers

Get List of Modified Objects within Entity Framework 7

I am stumped - upgrading to Entity Framework 7 and I typically override the SaveChanges inside the DbContext to be able to get a list of all the Modified Objects before it changes. Ultimately I have a script that fires that tracks the previous…
kevin c
  • 795
  • 1
  • 14
  • 29
10
votes
1 answer

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

Given the following code: void MergeDbContext(DbContext aSourceDbContext, DbContext aDestinationDbContext) { var sourceEntities = aSourceDbContext.ChangeTracker.Entries().ToList(); foreach (DbEntityEntry entry in…
Steven T. Cramer
  • 1,508
  • 1
  • 18
  • 34
10
votes
3 answers

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

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…
user3315406
10
votes
3 answers

"Object reference not set to an instance of an object" - but nothing is null?

Yeah, you probably think; "God, another one?". Yes, another one. "Object reference not set to an instance of an object." I've been working with EF6 lately and after developing for some time, I found that a little bit more optimization was needed.…
Dion V.
  • 2,090
  • 2
  • 14
  • 24
10
votes
1 answer

DbContext.Entry performance issue

Following Julia Lermas book 'DbContext' on a N-Tier solution of keeping track of changes, I provided each entity with a State property and a OriginalValues dictionary (through IObjectWithState). After the entity is constructed I copy the original…
pexxxy
  • 489
  • 1
  • 6
  • 17