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
25
votes
5 answers

How to Refresh DbContext

I want to refresh all entities of my DbContext without recreating it, I tried the following and none of them make sense: var context = ((IObjectContextAdapter)myDbContext).ObjectContext; var refreshableObjects = (from entry in…
Mohsen
  • 4,000
  • 8
  • 42
  • 73
25
votes
3 answers

Multithreading Entity Framework: The connection was not closed. The connection's current state is connecting

So I have a windows service process that performs a workflow process. The back end uses Repository and UnitofWork Pattern and Unity on top of Entity Framework with the entities class generated from the edmx. I won't go into a whole lot of detail as…
Brandon
  • 830
  • 1
  • 15
  • 35
24
votes
2 answers

ExecuteStoreQuery with Dbcontext

I want to use ExecuteStoreQuery function of Entity Framework and I was wondered that my context variable didn't have ExecuteStoreQuery method. So, I discovered that it's a method of ObjectContext class, but I've already used DbContext for my…
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
23
votes
2 answers

Set a value comparer in ASP.NET Core 3.1

I have used "HasConversion" in my DBContext to define a JSonArray (Language/Value) and save it as a Text field for ages and It works like a charm, I added a new project to my solution, nothing changed but then I got a new error on adding migration…
23
votes
2 answers

Using Automapper to update an existing Entity POCO

I am using EF4 DbContext to provide the model for an ASP.NET MVC app. I use ViewModels to provide data to the views and Automapper to perform the mapping between the EF POCOs and the ViewModels. Automapper does a great job but I'm not clear the best…
Paul Taylor
  • 5,651
  • 5
  • 44
  • 68
22
votes
10 answers

'Unable to resolve service for type ¨Microsoft.entityFrameworkCore.DbContextOptions¨1[LibraryData.LibraryContext] while attempting to activate

I've run into a problem i cannot solve on my own, so I'm asking for help. I've recently began learning ASP .net core, and wanted to build a library where i have to have a database, login system and admin powers to be able to add and delete books…
BobbyGopnik
  • 333
  • 1
  • 2
  • 8
22
votes
6 answers

What the heck is a context?

I'm starting to see Contexts everywhere I look. In ASP.NET MVC, there are ControllerContexts, RequestContexts, HttpContexts, FormContexts. In Entity Framework, you have ObjectContexts and DbContexts. Ninject has Ninject.Activation.IContext. What…
22
votes
6 answers

EF6 DBContext Dynamic Connection String

public partial class ProcessContext : DbContext { static ProcessContext() { Database.SetInitializer(null); } public ProcessContext() : base("Name=ProcessCS") //Comes from Config File { } …
Gautham Nayak
  • 241
  • 1
  • 3
  • 8
21
votes
5 answers

Using Entity Framework Core 3.1 with UseInMemoryDatabase option in ServiceProvider ( Scoped lifetime )

I have migrated a web application project from .NET Core 2.1 to 3.1 (also EF Core from 2.1.1 to 3.1.0). After the migration, some unit tests are not working anymore, throwing duplicate keys db exception. I simulated the problem and realize that EF…
Alin
  • 592
  • 1
  • 7
  • 21
21
votes
3 answers

Should I separate my application context from ApplicationDbContext used for identity?

In Visual-Studio 2013, when creating an ASP.NET project, it generates a file IdentityModels.cs that contains a class ApplicationDbContext, that inherits from IdentityDbContext, which eventually inherits from DbContext. Should I keep…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
21
votes
3 answers

What do I need to add into OnModelCreating(DbModelBuilder modelBuilder) function to define relations between Person and Role?

I'm using EntityFramework version 5.0 in WinForms project, .net 4.5. I have created 2 for me important Entities public class Role { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id {…
eCorke
  • 858
  • 1
  • 10
  • 24
20
votes
5 answers

How to Moq Entity Framework SqlQuery calls

I've been able to mock DbSet's from entity framework with Moq using this link. However, I would now like to know how I could mock the call to SqlQuery. Not sure if this is possible or how as it relies on the mocked db context knowing what "query" is…
David
  • 15,150
  • 15
  • 61
  • 83
20
votes
1 answer

The operation cannot be completed because the DbContext has been disposed using MVC 4

I know that this Question is asked so many times. I have read and implemented all solution but didn't get success. I am getting this error when I retrieve data from database using EF and binds with model after that use this model on View. My…
Sandeep Shekhawat
  • 685
  • 1
  • 9
  • 19
19
votes
2 answers

Entity Framework code first: How to ignore classes

This is similar to questions here and here, but those are old and have no good answers. Let's say I have the following classes: class HairCutStyle { public int ID { get; set; } public string Name { get; set; } } class…
Chris
  • 443
  • 1
  • 3
  • 14
19
votes
5 answers

Entity Framework relationships between different DbContext and different schemas

So, I have two main objects, Member and Guild. One Member can own a Guild and one Guild can have multiple Members. I have the Members class in a separate DbContext and separate class library. I plan to reuse this class library in multiple projects…
ScubaSteve
  • 7,724
  • 8
  • 52
  • 65