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
442
votes
9 answers

One DbContext per web request... why?

I have been reading a lot of articles explaining how to set up Entity Framework's DbContext so that only one is created and used per HTTP web request using various DI frameworks. Why is this a good idea in the first place? What advantages do you…
Andrew
  • 11,068
  • 17
  • 52
  • 62
270
votes
11 answers

Entity Framework: One Database, Multiple DbContexts. Is this a bad idea?

My impression to date has been that a DbContext is meant to represent your database, and thus, if your application uses one database, you'd want only one DbContext. However, some colleagues want to break functional areas out into separate DbContext…
238
votes
17 answers

How to update only one field using Entity Framework?

Here's the table Users UserId UserName Password EmailAddress and the code.. public void ChangePassword(int userId, string password){ //code to update the password.. }
h3n
  • 5,142
  • 9
  • 46
  • 76
175
votes
24 answers

The entity type is not part of the model for the current context

I am getting into the Entity Framework, but I am unsure if I am missing a critical point in the code-first approach. I am using a generic repository pattern based on the code from https://genericunitofworkandrepositories.codeplex.com/ and have…
janhartmann
  • 14,713
  • 15
  • 82
  • 138
141
votes
15 answers

How to fix the datetime2 out-of-range conversion error using DbContext and SetInitializer?

I'm using the DbContext and Code First APIs introduced with Entity Framework 4.1. The data model uses basic data types such as string and DateTime. The only data annotation I'm using in some cases is [Required], but that's not on any of the DateTime…
Alex Angas
  • 59,219
  • 41
  • 137
  • 210
123
votes
7 answers

How to force Entity Framework to always get updated data from the database?

I am using EntityFramework.Extended library to perform batch updates. The only problem is EF does not keep track of the batch updates performed by the library. So when I query the DbContext again it does not return the updated entities. I found that…
Saravana
  • 37,852
  • 18
  • 100
  • 108
105
votes
4 answers

LINQ to Entities only supports casting EDM primitive or enumeration types with IEntity interface

I have the following generic extension method: public static T GetById(this IQueryable collection, Guid id) where T : IEntity { Expression> predicate = e => e.Id == id; T entity; // Allow reporting more…
Steven
  • 166,672
  • 24
  • 332
  • 435
100
votes
6 answers

Mocking EF DbContext with Moq

I'm trying to create a unit test for my service with a mocked DbContext. I created an interface IDbContext with the following functions: public interface IDbContext : IDisposable { IDbSet Set() where T : class; DbEntityEntry
Gaui
  • 8,723
  • 16
  • 64
  • 91
91
votes
8 answers

How can I log the generated SQL from DbContext.SaveChanges() in my Program?

According this thread, we can log the generated SQL via EF, but what about DbContext.SaveChanges()? Is there any easy way to do this job without any extra frameworks?
Masoud
  • 8,020
  • 12
  • 62
  • 123
81
votes
4 answers

Entity Framework 5 deep copy/clone of an entity

I am using Entity Framework 5 (DBContext) and I am trying to find the best way to deep copy an entity (i.e. copy the entity and all related objects) and then save the new entities in the database. How can I do this? I have looked into using…
kypk
  • 813
  • 1
  • 7
  • 4
78
votes
11 answers

c# entity framework: correct use of DBContext class inside your repository class

I used to implement my repository classes as you can see below public Class MyRepository { private MyDbContext _context; public MyRepository(MyDbContext context) { _context = context; } public Entity…
Errore Fatale
  • 978
  • 1
  • 9
  • 21
77
votes
6 answers

Entity Framework and calling context.dispose()

When should one call DbContext.dispose() with entity framework? Is this imaginary method bad? public static string GetName(string userId) { var context = new DomainDbContext(); var userName = context.UserNameItems.FirstOrDefault(x =>…
Sindre
  • 3,880
  • 2
  • 26
  • 39
77
votes
10 answers

How to set CommandTimeout for DbContext?

I am looking a way to set CommandTimeout for DbContext. After searching I found the way by casting DbContext into ObjectContext and setting value for CommandTimeout property of objectContext. var objectContext = (this.DbContext as…
Yara
  • 4,441
  • 6
  • 42
  • 62
72
votes
9 answers

DbContext discard changes without disposing

I have a desktop client application that uses modal windows to set properties for hierarchical objects. Since this is a client application and access to the DbContext is not threaded, I use a long-running context on the main Form that gets passed…
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
54
votes
2 answers

What is the best practice in EF Core for using parallel async calls with an Injected DbContext?

I have a .NET Core 1.1 API with EF Core 1.1 and using Microsoft's vanilla setup of using Dependency Injection to provide the DbContext to my services. (Reference:…
1
2 3
99 100