2,593 questions
1 vote
0 answers
61 views
ASP.NET Core background/singleton service — how should I access DbContext safely? [duplicate]
I have an ASP.NET Core 8 app with a long-lived service that refreshes an in-memory cache on a timer. I registered the service as a singleton (it’s a hosted background worker), and it needs to query ...
0 votes
1 answer
94 views
Why DBContecxt.Database.Exists() becomes extremely slow when data source (IP) is wrong, but still fast when other credentials incorrect? [duplicate]
I am using EF 6 DbContext.Database.Exists() method, i.e. its return value true/false, to detect if a given DB is available. If it is, I pull and process data, if not, I enter a message into the log ...
2 votes
1 answer
83 views
Using the same DbContext class definition for both a DbContext object and an IDbContextFactory object
I am using .NET 8 and Oracle EF Core. In my application, I need to use both AddDbContext<MyDbContext>() for normal per-request operations and AddPooledDbContextFactory<MyDbContext>() for ...
1 vote
1 answer
45 views
How to get data when need to join table and only select the latest item
I have this query in SQL: SELECT c.CompanyName, ( SELECT TOP 1 ct.CompanyTypeID FROM CompanyType AS ct WHERE ct.CompanyID=c....
1 vote
0 answers
82 views
Generic context factory using LightInject in .NET 8 with generics
I am creating a library for our department to access multiple resources. Each resource type is in its own project inside the large solution. Each project will be built into its own NuGet package and ...
0 votes
0 answers
65 views
EF Core throw an exception related to a schema where the queried entity is from another schema
I get this exception: The entity type 'TblCentrale' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. This is the code in ...
0 votes
1 answer
78 views
How is a scoped DbContext's connection handled?
When I implement an ASP.NET Core application, I may have several middlewares that must interact with the database in order to check something. All of them, and also the final endpoint, share a scoped ...
0 votes
0 answers
61 views
DbContext lifetime / dependency injection issue running multiple Web API tests in .NET 8
I've run into a sticky little problem with my Visual Studio 2022 Web API test project. All of tests run successfully separately (one at a time), but when I try to run them all together (bulk) I get ...
0 votes
0 answers
71 views
Cannot enlist in the transaction because a local transaction is in progress on the connection. Finish local transaction and retry
I am trying to use TransactionScope to make my db context operations spread across different files run under one transaction, so all or none save should happen. The problem is it is giving me this ...
0 votes
2 answers
77 views
The instance of entity type 'VALUE' cannot be tracked because another instance with the same key value for {'OrderId'} is already being tracked
I decided to add models to work with the database. That's how it was and working public sealed class DatabaseContext : DbContext { public DbSet<Payment> Payments { get; set; } = null!; I ...
0 votes
0 answers
66 views
Variable context in ASP.NET MVC (C#)
In my ASP.NET MVC project (written in C#), I want to use the right DbContext according some value from some condition. That DbContext should be available for all the methods of the controller. To do ...
0 votes
0 answers
43 views
.NET Framework & Entity Framework : not saving entity's nested collection related with foreign key
I'm having some issues with Entity Framework on .NET Framework. My app is designed as such: I have my class that inherits from DbContext and I have this property: public DbSet<User> Users { get; ...
1 vote
1 answer
178 views
How to dynamically access tables across multiple databases using Entity Framework Core?
In my company, we have a system with the following structure: Moldable hosts: servers are defined by their IP addresses Moldable databases: each database is created based on a country code Company-...
0 votes
1 answer
92 views
Dataflow thread safe db context
My app receives multiple files (50 files, each file with 1000 rows) and processes them in Dataflow blocks. In one of the blocks UploadToDatabaseBlock, I set MaxDegreeOfParalellism to 8. This creates ...
0 votes
1 answer
77 views
EF Core 8 : get child table count as a property of model
Ok, so back in the day we used the ADO and created entities, but now that Visual Studio Pro 2009 won't work with .NET 8 anymore, it seems to be the target for today's era. Anyways besides that I had a ...