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.

dbcontext
Filter by
Sorted by
Tagged with
1 vote
0 answers
207 views

System.InvalidCastException: Unable to cast object of type 'ApplicationDbContext' to type 'System.Data.Entity.Infrastructure.IObjectContextAdapter'

I recently upgraded my app to .net core 6 and now I am getting this error in line this code: public List<KeyValuePair<string, long>> GetKeys(EntityEntry entry) { var keys = new List<...
jonv562's user avatar
  • 293
1 vote
0 answers
247 views

No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method

I recently upgraded my app to .net core 6 and now I am getting this error on this.ChangeTracker in my code: public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : ...
jonv562's user avatar
  • 293
1 vote
1 answer
407 views

Entity Framework Core bidirectional relationship

how to make bidirectional relationship to work in Entity Framework Core? Dotnet 6 Ef Core 6.0.13 public class User { public int id { get; set; } public string username { get; set; } ...
reguieg younes's user avatar
0 votes
2 answers
132 views

VB.NET: Add DBContext with Dependecy Injection

I'm trying to consume in VB.NET some classes using Dependency Injection. In the Global.asax.vb, I have: ..... Dim hostBuilder As HostBuilder = New HostBuilder() _ .ConfigureServices(...
ExpRick's user avatar
  • 13
0 votes
1 answer
573 views

Using Dependency Injection from Azure Key Vault to grab a connection string for AddDbContextFactory

I'm trying to set up the service registrations for my ihostedservice app and would like to pull in a connection string that is located in the key vault. What I have is something like this: using IHost ...
Kevin Le's user avatar
0 votes
1 answer
290 views

Blazor Server app - Some services are not able to be constructed

I am trying to write a simple Blazor Server app (which I have done before) and I am getting a 'System.AggregateException' error when the app tries to build. Some services are not able to be ...
Spodey's user avatar
  • 3
1 vote
0 answers
71 views

How to use generic DbContext in a monolithic architecture?

I'm starting with the base architecture of a monolithic project, I'm trying to implement a generic DbContext within a layered architecture, specifically within the Common project, which will be the ...
Jose Reinaldo Ayala Arias's user avatar
0 votes
0 answers
26 views

How to get Entity Framework to create a "char" from an enum [duplicate]

I'm working on a C# application, based on Entity Framework. In order to create the database, this is what I do: context.Database.EnsureCreated(); context.Database.Migrate(); , where context is an ...
Dominique's user avatar
  • 16.8k
1 vote
1 answer
431 views

How to mock database for testing in xUnit?

I have the following DbContext: public class OrganizerDbContext : IdentityDbContext<AppUser> { public OrganizerDbContext(DbContextOptions<OrganizerDbContext> options) : base(...
Samo's user avatar
  • 23
0 votes
1 answer
2k views

Creating Repository Pattern that provides multiple DbContexts with Entity Framework in .Net Core Web API

I want to develop a structure that will support generic DbContexts in the .Net Core Web API project and can be used in the repository pattern. Mysql and PostreSql databases are sufficient for now. Can ...
Ahmet Kalem's user avatar
0 votes
1 answer
262 views

Entity Framework custom ModelBuilder failing to be created properly when using the DbContextOptionsBuilder UseModel method

I have a database context that I want to separate some logic from the OnModelCreating method in another file instead. My current onModelCreating method in AppDbContext looks like this: modelBuilder....
Yasir's user avatar
  • 1
0 votes
1 answer
698 views

Set null value to an int variable in database

I created my database with entity framework - code first. public class Customer:BaseEntity { public string Name { get; set; } public string Surname { get; set; } public string Email { get; ...
AlexDeSouza's user avatar
1 vote
1 answer
433 views

Blazor InputText with maxlength based on dbcontext Table Column size

I've been looking at how to automate a Blazor InputText maxlength attribute to a dbcontext table column size (without using DataAnnotation which repeats information) and have come up with a solution - ...
user avatar
0 votes
0 answers
387 views

How to implement the EF Core IDbContextFactory in a WPF application?

I'm developing a WPF application. I'm new at using the IDbContextFactory and I am struggling to implement it. I've spent a few hours on Google, but couldn't find any solution. The DbFactory is, in all ...
Sylvain's user avatar
  • 13
0 votes
1 answer
170 views

One to many .WithOne null Fluent API EF Core 6 (code first)

I've configured one to many relation between entity User and Role in my DbContext. When I'm creating user through POST I'm getting this response which is correct { "userId": 3, "...
Ondřej Halata's user avatar
0 votes
1 answer
488 views

Entity Framework Core 6 - Cannot insert explicit value for identity column in table 'Roles' when IDENTITY_INSERT is set to OFF

I currently have a problem with EF6 and Fluent API. When inserting into database where model have one to many relationship, I'm getting this error. I've checked some related topics, but I want to keep ...
Ondřej Halata's user avatar
0 votes
1 answer
310 views

EntityFrameworkCore writing unit tests - not able to pass mocked context

I'm having issues in writing a unit test for Get/Add methods on the Entities, not able to pass mocked context inside the DAO class for instantiation. Its database first approach. please find the ...
Krishnan's user avatar
  • 978
1 vote
1 answer
746 views

Is there a way to inject a DB connection string from a service instead of a configuration for EF-Core DbContextPool

My application stores the database connection strings in a secrets vault and not in the config file. I am attempting to use EF-Core DbContextPool with dependency injection and wondering if I can use ...
Anthony's user avatar
  • 913
0 votes
1 answer
145 views

Do methods executed within a EF DbContext Transaction inherit it?

When using EF and wrapping a conditional block in a transaction, as long as the DbContext is passed into a method inside the block, will it inherit the transaction that was started? Example, the ...
tom355uk's user avatar
1 vote
1 answer
348 views

Azure Functions DbContext request delays - Connect to database at startup instead?

I have an Azure Functions app where I'm trying to eliminate delays in requests as much as possible. To combat cold start times, we've upgraded our Azure Functions plan to ensure we generally have one ...
JacobJ's user avatar
  • 3,717
0 votes
1 answer
797 views

AddDbContext does not get initialized when running ASP.NET Web API

I'm in the process of building a user management microservice using an API.NET Web API. I've added EntityFrameworkCore and using SQL Server as the database. I've made multiple services at this point, ...
Lasse Jonassen's user avatar
1 vote
0 answers
47 views

How can i register a DbContext with MySQL in .NET 7 [duplicate]

I want to migrate my project to .NET 7 and I don't know how to connect to my MySQL Database. Before my project was on .NET 6 and I used Pomelo to connect to my database. Is there currently a Nuget ...
Ronnie's user avatar
  • 47
0 votes
1 answer
38 views

Having trouble with intial migration to database

I created my SQL server and I can log in to it fine with those credentials in management studio. Excuse me if i said anything that doesnt make sense im new to this topic and this is my first stack ...
Mario De La Cruz's user avatar
0 votes
1 answer
360 views

error when updating the same item 2 times

I have the following code, where I receive an incomplete entity from the frontend, make changes and update it. Afterwards, I retrieve the same entity, now complete, from the database, make new changes ...
DeividCamargos's user avatar
0 votes
1 answer
2k views

Startup.cs is not showing in Visual Studio 2022? where can I find

I need to add DbContext in startup.cs, Can any one suggest DbContext
Bharath Kumar G's user avatar
0 votes
1 answer
83 views

Devart Error when I return database object with IActionResult

I am trying to return the database object that I get from my service in an IActionResult API Call (c# web API project). Whenever I attempt to do that I get this error: System.Text.Json.JsonException: ...
austin_wilcox21's user avatar
1 vote
1 answer
244 views

EntityFramework DB First Keyword not supported: 'port'

I want make model using db-first entity framwork. so i enter the command like picture below. but the error message shown as Keyword not supported: 'port'. how do i fix this issue. thanks in advance.
JH. Lee's user avatar
  • 27
0 votes
0 answers
33 views

Why context update another entity?

I have a logger service which I use to add the actions into a table, for each endpoint. For an unknown reason, when I use await context.SaveChangesAsync() in this service, it will update another table ...
AlleXyS's user avatar
  • 2,548
0 votes
1 answer
179 views

Creating DbContext instance inside LINQ

Just a quick question I thought I will ask to confirm I'm not introducing unnecessary trouble to my code. Is it ok to create a IDbContextFactory object directly inside LINQ, which is inside viewmodel'...
Donatas's user avatar
  • 327
1 vote
1 answer
1k views

EF Core error : duplicate key value violates unique constraint

I am getting this error when I try to Add an entity to the database after I switched from injecting the DbContext directly into the DI to using DbContextFactory: Microsoft.EntityFrameworkCore....
mihnea2kx's user avatar
  • 110
-1 votes
1 answer
397 views

Data context cannot see tables in db, EF Core in WPF

I try to create WPF application using Entity Framework Core. I have created my database (all of tables are correctly configured in SQL Server). I wrote my DbContext like below: internal class ...
Giusek keisuG's user avatar
0 votes
1 answer
314 views

how to add a single record in to table which has foreign key using EF code first approach

I have two tables ToDo ToDoId [PK], Title, description, Category , AssignedDate , DueDate TaskReview ReviewId, Rating , Comment, ToDoId [FK] -i have to add ...
geetha 's user avatar
0 votes
1 answer
1k views

The connection does not have any active transactions

I just installed .NET 7 SDK and updated csproj from <TargetFramework>net6.0</TargetFramework> to <TargetFramework>net7.0</TargetFramework> and updated corresponding ...
PetrK's user avatar
  • 101
1 vote
1 answer
1k views

.NET 6 dependency injection and DbContext

I want to use dependency injection together with DBContext across several class libraries. In program.cs I could have this line of code: services.AddDbContext<MyDbContext>(o=>o.UseSqlServer(&...
Thomas's user avatar
  • 31
0 votes
1 answer
210 views

EF Core - Reuse Table entity

I have a database, which exists on 3 different stage servers. The database is identical on all three servers. I write an application to sync the database tables based on some logic. For that approach ...
5p1k3md's user avatar
  • 65
0 votes
1 answer
293 views

Is There a Way To Register Services.AddDbContext In ApplicationBbContext Out-Side The Program.Cs In DotNet 6

The Repository Failed To Construct Because The DbContext Does Not registered. ApplicationDbContext protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { ...
Mina Emad Radi's user avatar
0 votes
2 answers
525 views

Entity Framework Core - I can't add a new migration since I added a new table (Unable to resolve service)

I am facing a problem since I added a new table to my DbContext: I cannot add a new migration. I have 42 tables in my DbContext and the migrations were working fine until I added the 42nd one. When I ...
Dylan's user avatar
  • 11
0 votes
1 answer
311 views

Configuration for Database Connection

Please I'm new to C# and I've been trying to configure my code to work with a database. I need a proper and step by step guide on how to go about it. Thanks I have created a class that inherits from ...
tzu's user avatar
  • 1
0 votes
1 answer
274 views

EFCore: Is it possible to track changes made to the properties of a newly created and added to the DbContext entry? I'mNOT asking for EntryState.Added

public class Person { public int Id { get; set; } public string LastName { get; set; } public string FirstName { get; set; } public DateTime DOB { get; set; } public Person() ...
NAngelGr's user avatar
0 votes
1 answer
1k views

EFCore: How to detect which of the newly added entity's properties have changed since the dbContext Attach or Add happened?

(trying to make my query clearer and not to confuse you): In a WinForm app that makes use of EFCore, C# and SQLite, What I am trying to achieve is this: I have a WinForm called AddNewVisitForm. I use ...
NAngelGr's user avatar
0 votes
0 answers
103 views

EF: PROBLEM WITH DBCONTEXT ACCESS FROM 2 DIFFERENT System.Timers.Timer

I have a problem with my console app. I have a main static class contains the dbcontext as public property. When i start the app, my static class execute the following method: ` private static void ...
Luca Pannozzo's user avatar
0 votes
0 answers
46 views

INSERT statement conflicted with the FOREIGN KEY(confilct with table not included in context build)

I am getting the following error. Could you please help me? In addition i want to mention that the FISCALYEAR table is not selected in my EF CORE POWER TOOLS the tool that i use to build my DBContext. ...
Sotiris's user avatar
  • 38
0 votes
1 answer
188 views

Store foreign key depending on the enum

Few days ago I switch (due to my student project) to Entity Framework and I have to develop Entity whos foreign key will depend of ENUM value , I spend last two day trying to figure it out but ...
mawerick_1's user avatar
-1 votes
2 answers
658 views

Share context between unit test and web api in a C# project

How can I share the context that the test project created while running with the context of the web api? If the web api creates its context from environment variable, that will be different to that ...
Jaczura Zoltán's user avatar
-1 votes
1 answer
1k views

DbContext scope when calling a handler from another handler

I have a fairly large application where one handler will call another handler. I have read how this is not something we should be doing: Calling one handler from another one, is it good? Dealing with ...
user avatar
0 votes
1 answer
24 views

Context data not passing to Django templates

My cart view. I have passed context on return but it doesn't appear on the templates. If i print the def cart(request): total = 0 quantity = 0 cart_items = None tax ...
Sohanoor Rahman's user avatar
0 votes
0 answers
90 views

C# Update local table object after adding new record using entity framework

I am working with C# and Pgadmin (Database). Below is my scenario- I am fetching the "Student" table from db (by using dbContext). And along with that I am fetching/including the table "...
sunil's user avatar
  • 145
-1 votes
1 answer
109 views

Unable Scaffold To Razor view

Unable to resolve service for type 'Microsoft EntityFrameworkCore DbContextOptions`1[DonnaPerfum DataAccess Data ApplicationDbContext] while attempting to activate DonnaPerfum DataAccess Data ...
Razor's user avatar
  • 1
1 vote
1 answer
248 views

How to use DBContext inside WebApplicationFactory in my MSTest class/method?

I have CustomWebApplicationFactory class public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<Program> { private static string conn = ""; //my ...
tickwave's user avatar
  • 3,397
0 votes
0 answers
334 views

DbContext Concurrent Access Issue

I have this query var user = await DbContext.Set<ApplicationUser>() .FirstOrDefaultAsync(x => x.UserID.ToUpper() == userID.ToUpper()); that throws the following exception ...
Betsegaw Lemma Amersho's user avatar

1 2 3
4
5
52