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
37
votes
2 answers

Why re-initiate the DbContext when using the Entity Framework?

I don't know if there is a better way to use the DbContext because it is not recommended to set is as static when working with WCF. So we are creating it each time we want to access the database. Knowing all the advantages of using Entity Framework,…
Boomer
  • 1,468
  • 1
  • 15
  • 19
37
votes
1 answer

DbSet.Find method ridiculously slow compared to .SingleOrDefault on ID

I have the following code (Database is SQL Server Compact 4.0): Dim competitor=context.Competitors.Find(id) When I profile this the Find method takes 300+ms to retrieve the competitor from a table of just 60 records. When I change the code to: Dim…
Dabblernl
  • 15,831
  • 18
  • 96
  • 148
35
votes
5 answers

How can I log all entities change, during .SaveChanges() using EF code first?

I'm using EF code first. I'm using a base Repository for all my repositories and an IUnitofWork that inject to the repositories, too: public interface IUnitOfWork : IDisposable { IDbSet Set() where TEntity : class; int…
Masoud
  • 8,020
  • 12
  • 62
  • 123
35
votes
3 answers

unexpected GetType() result for entity entry

While I iterating through ObjectStateEntries I expected [t] variable name will be MY_ENTITY foreach (ObjectStateEntry entry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted)) { Type t = entry.Entity.GetType(); …
33
votes
3 answers

Configure multiple database Entity Framework 6

In my solution I have 2 projects that use Entity Framework 6. Each points to a different database, both using the same data provide - SQL Server. A third project in my solution needs to use both databases. My problem is how to configure those…
Motty
  • 579
  • 1
  • 6
  • 9
32
votes
2 answers

Removing many to many entity Framework

There is a many to many relationship between Artist and ArtistType. I can easily add artist ArtistType like below foreach (var artistType in this._db.ArtistTypes .Where(artistType => vm.SelectedIds.Contains(artistType.ArtistTypeID))) { …
akd
  • 6,538
  • 16
  • 70
  • 112
32
votes
2 answers

EF (entity framework) usage of "using" statement

I have a project on MVC. We chose EF for our DB transactions. We created some managers for the BLL layer. I found a lot of examples, where "using" statement is used, i.e. public Item GetItem(long itemId) { using (var db = new…
Pal
  • 756
  • 2
  • 10
  • 20
30
votes
3 answers

What is the best way to instantiate and dispose DbContext in MVC?

MVC 3 + EF 4.1 I'm choosing between two approaches to deal with DbContext: Instantiate in Application_BeginRequest, put it into HttpContext.Current.Items and dispose in Application_EndRequest. Create disposable UnitOfWork (kindof wrapper for…
YMC
  • 4,925
  • 7
  • 53
  • 83
30
votes
2 answers

Is it possible to query Entity Framework before calling DbContext.SaveChanges?

In this simple example, I have two entities: Event and Address. I have a console application running every night to import event data from an XML source and add it to my database. As I loop through the XML event nodes (inside of the Entity Framework…
29
votes
1 answer

Most efficiently handling Create, Update, Delete with Entity Framework Code First

Note: I am using Entity Framework version 5 Inside my generic repository, I have Add, Edit and Delete methods as below: public class EntityRepository : IEntityRepository where T : class, IEntity, new() { readonly DbContext…
tugberk
  • 57,477
  • 67
  • 243
  • 335
27
votes
1 answer

Why must I have a parameterless constructor for Code First / Entity Framework

This is more of a question of "Why we do things" as my actual problem was solved but I don't know why. I was dealing with the following code inside my CountyRepository: public IEnumerable GetCounties(string stateAbbr) { using…
Travis B
  • 373
  • 1
  • 3
  • 5
27
votes
1 answer

Does Entity Framework's DbContext save changes if no changes were made?

I could not find an answer on the Internet. Let's suppose I have a DbContext, and I just select all the entities from it. I don't add, update or delete any entity on the DbSet. If I call SaveChanges afterwards on the DbSet. Does it actually waste…
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
26
votes
8 answers

How do you configure the DbContext when creating Migrations in Entity Framework Core?

Is there way that dependency injection can be configured/bootstrapped when using Entity Framework's migration commands? Entity Framework Core supports dependency injection for DbContext subclasses. This mechanism includes allowing for configuration…
vossad01
  • 11,552
  • 8
  • 56
  • 109
25
votes
3 answers

Using TransactionScope with Entity Framework 6

What I can't understand is if its possible to make changes to the context and get the changes in the same transaction before its commited. This is what I´m looking for: using (var scope = new TransactionScope(TransactionScopeOption.Required)) { …
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69
25
votes
4 answers

One transaction with multiple dbcontexts

I am using transactions in my unit tests to roll back changes. The unit test uses a dbcontext, and the service i'm testing uses his own. Both of them are wrapped in one transaction, and one dbcontext is in the block of the other. The thing is, when…
Mark Homans
  • 637
  • 1
  • 6
  • 12