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

Entity Framework, Code First, Update "one to many" relationship with independent associations

It took me way too long to find a solution to the scenario described below. What should seemingly be a simple affair proved to be rather difficult. The question is: Using Entity Framework 4.1 (Code First approach) and "Independent associations" how…
Martin
  • 185
  • 1
  • 1
  • 10
13
votes
1 answer

DbContext won't keep connection open for re-use

I'm trying to reuse an existing database connection so that I can do multiple database operations using a TransactionScope without invoking MSDTC. Entity Framework (using the new DbContext API in the 4.1 release) doesn't seem to want to keep an…
GWB
  • 2,575
  • 2
  • 23
  • 28
13
votes
2 answers

Entity Framework Code First: which DataType attribute for DateTime2?

Sometime when using Entity Framework Code First, the default conventions do not create the database type you want. For instance by default a property of type System.DateTime creates a database column of type DateTime. What to do if you want it to…
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
13
votes
4 answers

How do I stop Entity Framework from generating BOTH ObjectContext and dbContext

Background Visual Studio 2012 NEW Model.EDMX file created in VS2012 Copied some of the EDMX xml from a previously created EDMX into the new one Problem / Question Now. The EDMX (TT transform, custom tool, whatever, etc.) is generating BOTH…
Jay Stevens
  • 5,863
  • 9
  • 44
  • 67
12
votes
2 answers

Why does Entity Framework automatically use the ObjectContext instead of the DbContext when mapping database tables using ADO.NET Entity datamodel

I am following the database approach first; I have created the tables in my SQL Server 2008 database, then I map those tables to Entity Framework classes using an ADO.NET Entity Data Model. But when I opened the designer.cs file I found the…
12
votes
1 answer

DbContext SaveChanges Order of Statement Execution

I have a table that has a unique index on a table with an Ordinal column. So for example the table will have the following columns: TableId, ID1, ID2, Ordinal The unique index is across the columns ID1, ID2, Ordinal. The problem I have is that when…
12
votes
3 answers

Can I clone an IQueryable to run on a DbSet for another DbContext?

Suppose I have built up, through some conditional logic over many steps, an IQueryable instance we'll call query. I want to get a count of total records and a page of data, so I want to call query.CountAsync() and…
Triynko
  • 18,766
  • 21
  • 107
  • 173
12
votes
2 answers

How to use a dbcontext in a static class? (ObjectDisposedException)

Hey I just recently learnt how to use extension methods and pretty excited to implement it in my current project. My objective: I want to check whether an entry exists in my table in a helper class, since I'm going to use it in multiple controllers…
12
votes
1 answer

IdentityServer4 PersistedGrantDbContext & ConfigurationDbContext

New to IdentityServer 4. I followed the IdentityServer4 EntityFramework sample here on the documentation. After the migration script is run dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o…
12
votes
2 answers

Correct use of Microsoft.AspNet.Identity 2.0

I'm lost using the authentication method that comes with MVC 5 Template. I had the need to include the CreateBy user in an entity called client, so after some research I came to this: Model: [Table("Clients")] public partial class Client { …
Patrick
  • 2,995
  • 14
  • 64
  • 125
12
votes
1 answer

How Can I Use Custom Validation Attributes on Child Models of a DB Entity?

Summary: I want a data annotation validator to reference another property in the same class (TitleAuthorAndPublishingConfiguration). However, DB.SaveChanges() is not being called on this class directly. Rather it is being called on the parent of…
Martin Hansen Lennox
  • 2,837
  • 2
  • 23
  • 64
12
votes
1 answer

Using multiple dbcontext instances and dependency injection

This is kind of a similar question I asked here a few weeks ago with one significant change in requirement. I have a new and unique (I have not found anything like this in my stackoverflow search) business requirement: I have created two separate…
Babu Mannavalappil
  • 447
  • 2
  • 9
  • 27
12
votes
5 answers

Manage the lifetime of dbContext

I would like to tie the lifetime of a dbContext to the lifetime of a session, to - for example - be able to commit or abandon changes on a group of mutations on the dbcontext over multiple requests. Are there any other (better?) ways to accomplish…
Martijn
  • 11,964
  • 12
  • 50
  • 96
12
votes
2 answers

How to convert DbSet in Entity framework to ObjectQuery

I am using CodeFirst approach and struck with an issue where I need to convert DbSet to ObjectQuery. This is what I did for conversion. ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext; ObjectSet objectSet =…
11
votes
2 answers

.net Core 2, EF and Multi Tenancy - Dbcontext switch based on user

I have the (almost) worst of multi tenancy. I'm building a asp.net core website that I'm porting a bunch of pokey little intranet sites to. Each subsite will be an asp.net Area. I have an IdentityContext for the Identity stuff. I have multiple…
Hecatonchires
  • 1,009
  • 4
  • 13
  • 42