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

Top per group: Take(1) works but FirstOrDefault() doesn't?

I'm using EF 4.3.1... just upgraded to 4.4 (problem remains) with database-first POCO entities generated by the EF 4.x DbContext Generator. I have the following database named 'Wiki' (SQL script to create tables and data is here): When a wiki…
Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93
10
votes
1 answer

Is it always better to use 'DbContext' instead of 'ObjectContext'?

I just downloaded EntityFramework.dll v4.3. I've found a number of questions that compare DbContext vs. ObjectContext. But most of these are from 2010, or early 2011. I'd like to read more on the subject. Specifically, are there any books on…
bevacqua
  • 47,502
  • 56
  • 171
  • 285
9
votes
3 answers

EF and repository pattern - ending up with multiple DbContexts in one controller - any issues (performance, data integrity)?

Most of my knowledge of ASP.NET MVC 3 comes from reading through the book Pro ASP.NET MVC 3 Framework by Adam Freeman and Steven Senderson. For my test application I have tried to stick to their examples very closely. I am using the repository…
9
votes
6 answers

DbSet.FirstOrDefault()?

I'm trying to do this but it says I can't use FirstOrDefault, public static int GetId(this Context db, Type type, string name) { return db.Set(type).FirstOrDefault(x => x.Name == name).Id; } The error is 'System.Data.Entity.DbSet' does not…
Benjamin
  • 3,134
  • 6
  • 36
  • 57
9
votes
4 answers

How to setup readonly collection property with backing field in EF Core 2.2

I am trying to create an entity class which will expose a related collection through a readonly property, like this: public class MyEntity: Entity { public int Id{ get; private set; } private IList _relatedEntities = new…
vanpersil
  • 764
  • 1
  • 8
  • 26
9
votes
2 answers

Separate DbContext for Identity and business operations with same database - is there any advantages?

I've started working on an existing ASP.NET Core project that uses the Identity framework. The application uses a single database. For some reason the application uses two separate database contexts - one derived from IdentityDbContext which of…
atiyar
  • 7,762
  • 6
  • 34
  • 75
9
votes
1 answer

Convert DbContext to Datatable in Code first entity framework

Hello I am trying to convert DbContext result to DataTable. I have one class i.e. ClientTemplateModel which inherits DbContext. In this class I have one DbSet object i.e. public virtual DbSet ImageComments { get; set; }. I am using…
Ajay
  • 6,418
  • 18
  • 79
  • 130
9
votes
2 answers

How to specify the name of the table to be used by DbContext

This is a followback question from my earlier question. I was under the impression that if there're more than one table in the database, the name of the DbSet variable will be used to identify the table. However in the linked question it was clear…
0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
9
votes
3 answers

Could not load type 'System.ComponentModel.DataAnnotations.Schema.IndexAttribute'

I am having this Error. The ApplicationDbContext is Auto Generated DbContext, used in account section. Whenever i try to use any Account Controller actions this error occurs. The error originates from public AccountController() :…
Ruchan
  • 3,124
  • 7
  • 36
  • 72
9
votes
2 answers

Using multiple DbContexts with a generic repository and unit of work

My application is getting larger and so far I have a single MyDbContext which has all the tables I need in my application. I wish (for the sake of overview) to split them up into multiple DbContext, like MainDbContext, EstateModuleDbContext,…
janhartmann
  • 14,713
  • 15
  • 82
  • 138
9
votes
5 answers

DbContext has been disposed

I developed a web application with ASP.NET MVC 4 and SQL Server 2008, I create ContextManager class to have only one database context in all pages. public static class ContextManager { public static HotelContext Current { get …
Ali Esfahani
  • 119
  • 1
  • 3
  • 7
9
votes
1 answer

Entity Framework Code First: SaveChanges is not atomic

I have the following very simple unit test that reproduces a case where DbContext.SaveChanges is not atomic. By not atomic I mean that the committed data can be read before all the commit was completed. Add task: In a loop, adds a new TestEntity and…
Ohad Meir
  • 714
  • 8
  • 18
9
votes
1 answer

How to update an entity's navigation properties in Entity Framework

In ASP .NET MVC 3 with Entity Framework, I have a domain object which has a navigation property referencing another objects, as follows: public class Person { public String Name {get;set;} public Guid CompanyID{get;set;} …
8
votes
1 answer

DbContext SaveChanges() - Detecting updated entities

I have overridden the SaveChanges() method in the Entity Framework 4.1 DbContext class. My override looks like this: public override int SaveChanges() { IEnumerable modifiedEntityEntries = ChangeTracker.Entries().Where( e =>…
FantasticJamieBurns
  • 3,004
  • 3
  • 22
  • 21
8
votes
1 answer

ASP.NET MVC 3 EF CodeFirst - DBContext item editing

I have been frustrated by this for like a day...please help. I have an invoice section in which I'm able to add new items dynamically by appending/removing DOM elements using JQuery...i have already figured out how to name these elements correctly…
Mirek
  • 4,013
  • 2
  • 32
  • 47