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:
- MSDN Class Definition
- ADO.NET Blog - Using DbContext in EF 4.1: 12 Part Series

2191 questions
415
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
  • 10,088
  • 14
  • 48
  • 62
235
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…
203
votes
16 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
  • 4,786
  • 9
  • 40
  • 70
160
votes
22 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
  • 13,440
  • 13
  • 78
  • 130
138
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
  • 56,458
  • 39
  • 130
  • 203
97
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
  • 151,500
  • 20
  • 287
  • 393
94
votes
5 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
  • 28,153
  • 11
  • 81
  • 94
86
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
  • 7,580
  • 9
  • 51
  • 108
84
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
  • 7,936
  • 13
  • 59
  • 86
77
votes
10 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
  • 970
  • 1
  • 8
  • 18
76
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
  • 763
  • 1
  • 6
  • 4
72
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,780
  • 2
  • 24
  • 39
70
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,063
  • 6
  • 38
  • 60
66
votes
8 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
  • 13,199
  • 12
  • 71
  • 154
52
votes
1 answer

EF Code First DBContext and Transactions

I would like know what is the best possible way to implement transactions with DBContext. In particular, Does DbContext.SaveChanges implement transaction internall if i change multiple entities? If i want to call DbContext.SaveChanges multiple…
1
2 3
99 100