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
19
votes
4 answers

Is DbSet<>.Local something to use with special care?

For a few days now, I have been struggling with retrieving my entities from a repository (DbContext). I am trying to save all the entities in an atomic action. Thus, different entities together represent something of value to me. If all the…
bas
  • 13,550
  • 20
  • 69
  • 146
19
votes
8 answers

The provider did not return a ProviderManifestToken string error

I am trying to create a web app using ASP.Net MVC3, Entity Framework and MySQL. I have added the following code to my Web.Config file.
Harshani
  • 649
  • 2
  • 9
  • 22
18
votes
2 answers

Share DbContext across Repositories in MVC Web App

Question What is the proper way to share an EF DbContext across multiple repositories in an MVC web app? Is it prudent/necessary to do so, what are the pitfalls of doing or not doing this? Background Assume: App.MvcSite (Several dozen controllers,…
17
votes
3 answers

SqlException: Syntax Error Near 'GO'

I am having trouble sending a SQL statement through a DbContext using context.Database.ExecuteSqlCommand(). I am trying to execute CREATE TABLE Phones([Id] [uniqueidentifier] NOT NULL PRIMARY KEY, [Number] [int],[PhoneTypeId] [int]) GO ALTER…
bdparrish
  • 3,216
  • 3
  • 37
  • 58
17
votes
2 answers

What is the difference between IDbSet.Add and DbEntityEntry.State = EntityState.Added?

In EF 4.1+, is there a difference between these 2 lines of code? dbContext.SomeEntitySet.Add(entityInstance); dbContext.Entry(entityInstance).State = EntityState.Added; Or do they do the same thing? I'm wondering if one might affect child…
danludwig
  • 46,965
  • 25
  • 159
  • 237
17
votes
1 answer

Entity Framework 6 - use my getHashCode()

There's a certain amount of background to get through for this one - please bear with me! We have a n-tier WPF application using EF - we load the data from the database via dbContext into POCO classes. The dbContext is destroyed and the user is then…
Buzby
  • 573
  • 5
  • 13
16
votes
3 answers

'ObjectContext' vs 'DbContext' in Entity Framework

I'm using the DbContext class within code that I am creating that is based on the Generic Repositories and Unit of Work design patterns. (I am following the guidance here.) While working on this project I have encountered the ObjectContext class.…
afr0
  • 848
  • 1
  • 11
  • 29
16
votes
1 answer

EF 4.1 OnModelCreating not called

I have a problem with EF 4.1 not calling OnModelCreating so that I can configure tables etc. I have an existing database. Here is my connection string:
ASR
  • 429
  • 3
  • 6
  • 16
16
votes
12 answers

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

I am having this issue updating my database 1 column at a time in asp.net using web api. I am trying to query a PUT to just update one value in the row instead of updating that one and setting the rest to null. I made a separate model outside of the…
Shawn
  • 2,355
  • 14
  • 48
  • 98
15
votes
5 answers

"No executable found matching command dotnet-ef" error with EF Core database-first

As you know, the newest version of Visual Studio 2017 abandons the 'project.json' and uses .csproj instead. I'm using the RTM version and want to generate model from an exist database, following this guide. I got an error on the last step: The…
even
  • 315
  • 4
  • 11
15
votes
5 answers

"..." cannot implement an interface member because it is not public

public interface IDatabaseContext : IDisposable { IDbSet Entities1 { get; set; } } public class MyDbContext : DbContext, IDatabaseContext { IDbSet Entities1 { get; set; } } Can't compile because of the error…
Acrotygma
  • 2,531
  • 3
  • 27
  • 54
15
votes
3 answers

Why is DbContext.SaveChanges 10x slower in debug mode

Can somebody explain Why does DbContext.SaveChanges run ~10x slower in debug mode than production mode? Is there any way I can speed this up? In debug mode, my webpage takes 116 seconds to load versus 15 seconds if I start the project without…
Jesse
  • 1,673
  • 1
  • 16
  • 22
14
votes
2 answers

Avoid Entity Framework Error with Multiple Tasks Running Concurrently on Same DbContext

I have a WebApi controller in a Dotnet Core project running Entity Framework Core with Sqlite. This code in an action occationally produces errors: var t1 = _dbContext.Awesome.FirstOrDefaultAsync(a => [...]); var t2 =…
Oskar Lindberg
  • 2,255
  • 2
  • 16
  • 36
14
votes
3 answers

Replace entity in context with a different instance of the same entity

I have an entity which is not connected to my dbcontext. I want to change that. However there is already another instance of the same entity attached to dbcontext. If I just add my new entity, I get an error, that an entity with the same primary key…
Tim Pohlmann
  • 4,140
  • 3
  • 32
  • 61
14
votes
4 answers

LINQ many-to-many relationship, how to write a correct WHERE clause?

I use many-to-many relationship for my tables. There is a query: var query = from post in context.Posts from tag in post.Tags where tag.TagId == 10 select post; Ok, it works fine. I get posts having the tag specified by id. I have a…
Jean Louis
  • 1,485
  • 8
  • 18
  • 33