23,541 questions
0
votes
0
answers
35
views
EF Core throwing up random and unpredictable errors
I'm using .NET 9. I have a query running at night. It runs in a separate thread depending on the CPU count. If there are 4 physical CPU, it will spawn 4 threads and run concurrently.
I have ensured ...
0
votes
1
answer
57
views
Add Parent and Child entity in the same context.SaveChanges [closed]
I have a lookup item and a parent entity that I want to add in the same transaction.
So for instance, I use this code:
foreach (ParentDto parentdto in ParentList)
{
Parent newParent = new Parent();...
0
votes
1
answer
146
views
Entity Framework problems with grouping and sorting by many fields
I have a method, that returns a value of type List<IGrouping<PGGroupingKey, PGroupingResult>>.
The request code and description of the types are below
internal class PDGrouppingKey
{ ...
1
vote
1
answer
84
views
EF Core Table Per Hierarchy: Query with base type in child element "Translation of member 'Parent' on entity type 'Base' failed"
I am trying to write a query like the following with a similar model. I think I understand the problem, but have not been able to edit the model in a way that doesn't add new foreign key columns to ...
2
votes
1
answer
51
views
How do I share a foreign key in an EF Core TPH hierarchy?
I have an Events base class with an abstract child class which itself has two (for now) child classes:
public class Event {
}
public abstract class FooEvent : Event {
public Foo? Foo { get; set; }...
1
vote
1
answer
53
views
Npgsql EF Core does not map enum to PostgreSQL enum type (sent as integer instead)
I'm trying to map some enums in my model to PostgreSQL custom enum types using EF Core, but no matter what I do, EF Core keeps sending the values as integers instead of using the PostgreSQL enum type.
...
0
votes
1
answer
78
views
How to prevent Entity Framework Core from using Table-Per-Hierarchy (TPH) mapping
I use MongoDB.EntityFrameworkCore.
I have 3 classes: Pod, Planet, Asteroid with inheritance from SpaceObject
I want use the default Table-Per-Hierarchy (TPH) mapping for Planet and Asteroid to save ...
1
vote
1
answer
111
views
ASP.NET Core HealthCheck wants a database migration, but I can't figure out what migration it wants
My ASP.NET Core app is throwing this exception, and I can't figure out how to resolve it:
An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The ...
-1
votes
1
answer
137
views
How to filter rows in an Include and ThenInclude statements in LINQ and C#
I'm trying to populate an object with two collections of type ChatSession.
ChatSession contains two types of sessions, Open and Solved.
Edit: Query changed to fit solution from Charlieface.
Add<...
2
votes
1
answer
46
views
PendingModelChangesWarning When Starting API in Docker Container
I have an ASP.NET Core 9 Web API and database in SQL Server. I'm trying to run them in containers, and have the API connect to the database. I have been developing this locally for a while and ...
0
votes
1
answer
50
views
NSubstitute: ToDictionaryAsync: Only sources that implement IDbAsyncEnumerable can be used for Entity Framework asynchronous operations
I'm using .NET 8 with NSubstitute for unit tests.
Even though I setup my repository using IQueryable and BuildMock correctly:
when calling ToListAsync, unit test is working fine
when calling ...
0
votes
1
answer
76
views
Directus schema apply fails with "Field already exists" when using existing tables created by EF Core
I'm trying to use Directus as a read-only admin UI for an existing PostgreSQL database where all tables are created and managed by EF Core (.NET) migrations. I want Directus to only handle UI ...
3
votes
0
answers
57
views
HotChocolate ISelection drops child collection JOINs in EF Core when projecting to a view model
I am hitting a wall combining HotChocolate's projection engine (ISelection) with Entity Framework Core when trying to project database entities into custom view models / DTOs.
Whenever I append ...
4
votes
2
answers
154
views
Atomic JSONB array append in PostgreSQL with UPDATE ... RETURNING – can concurrent updates appear in the returned row?
I’m implementing an atomic append operation on a jsonb array column in PostgreSQL and want to understand the concurrency behavior of UPDATE ... RETURNING.
My table has:
"Thumbnails" jsonb ...
3
votes
1
answer
132
views
EF Core 10 cold start after idle — is using a hosted service for keep-alive acceptable?
I have an ASP.NET Core 10 application using EF Core 10 with SQL Server 2017.
Problem:
Queries work normally when the app starts.
After the app is idle for a few minutes, all EF Core queries become ...
2
votes
2
answers
112
views
EF Core scaffold - unique index with WHERE condition produces a 1:1 mapping instead of 1:n
We are using .NET 10 and the following NuGet packages:
Microsoft.EntityFrameworkCore 10.0.3
Microsoft.EntityFrameworkCore.Design 10.0.3
Npgsql.EntityFrameworkCore.PostgreSQL 10.0.0
We have a simple ...
Best practices
4
votes
3
replies
124
views
Am I safe to merge Entity Framework Core migrations added by "feature branches"?
I have a workflow I use with our development team that involves merging EF Core migrations in our C# project. Our team works in feature branches and often needs to create new EF Core migrations. They ...
Best practices
1
vote
1
replies
95
views
How to securely implement JWT authentication in a .NET health monitoring app using MySQL?
I’m developing a web application for pharmacovigilance similar to VAERS and some UN health reporting dashboards. The app is built with .NET 8, Entity Framework Core, and MySQL.Users are medical ...
Best practices
0
votes
2
replies
128
views
Using Entity Framework Core with PostgreSQL
Can I use a database-first approach to generate models initially, then move to code-first later for easier updates? What's the best way to do this?
If so, what is the recommended workflow, and what ...
1
vote
0
answers
66
views
EF Core database first scaffold-dbcontext generate model with different name
I am creating a REST API, and I am using EF Core 9. I have the database and the tables. I want to run database first to import the tables into the application.
I ran this command:
Scaffold-DbContext &...
Advice
1
vote
5
replies
112
views
EF Core tracking vs. caching
We have a large scale application that was ported to .NET and contains a lot of legacy code in stored procedures.
Mixing EF Core with stored procedures is causing troubles in some scenarios, due to ...
5
votes
2
answers
194
views
Are there some ways to instruct EF Core to generate query in a particular way?
I understand that the main purpose of EF Core is to let the developer work with a database without paying attention to all the specifics of database programing, and let him just work with plain .NET ...
0
votes
0
answers
77
views
EF Core tries to create the database every time it is run on docker compose
I have an ASP.NET Core 9 Web API project running with SQL Server. It has been developed so far locally, only with SQL Server running in Docker. I've decided to move the API project to Docker, ...
5
votes
0
answers
151
views
Why does EF Core create a new shadow foreign key instead of using an existing property?
I have two model classes, Entity and EntityVersion (the name Entity here is unrelated to EF, it's just coincidence given my company's domain). Their (simplified) models have a few relationships:
One ...
1
vote
0
answers
44
views
EF Core startup migrations not applying when deployed to Azure
I am developing an ASP.NET Core Web API and have the following code in my Program.cs:
// Apply migrations / create database on startup
using (var scope = app.Services.CreateScope())
{
var logger = ...
0
votes
0
answers
116
views
When using lambda expressions with bulk update APIs for EF Core, is the lambda expression evaluated in-memory, or on the DB-side?
I am using the Bulk Update APIs, part of EF Core like the ExecuteUpdateAsync() functions, to perform updates, and I am passing in a lambda for my valueExpression. I am wondering if the evaluation of ...
Best practices
0
votes
3
replies
86
views
"Serializable" transactions vs guarded atomic UPDATE, concurrency racing conditions
I have been building a concurrent system for a while. In the past I found some articles about "serializable" transactions. I have been using code first EF Core primarily for any database ...
0
votes
1
answer
110
views
EF Core migrations fail in .NET 10 with "Telemetry is: Enabled" JSON parsing error - telemetry disable not working
After installing the .NET 10 SDK, I started getting an exception when trying to create migrations with Entity Framework Core.
The command I'm using:
dotnet ef migrations add MigrationName --startup-...
1
vote
1
answer
92
views
EF Core migrations do not generate AspNetUserPasskeys table when using custom IdentityDbContext .NET 10
I'm starting a new project with Blazor and authentication using .NET 10. I want to move my DbContext to a separate library. The DbContext has an additional parameter in its constructor:
public class ...
1
vote
0
answers
183
views
Common DLL for Entity Framework Core
I'm hoping to create a "common" DLL with common entities (i.e. Person, RMA, RMAFollowUp, etc) in that lib I code all the reference like RMAFollowUp entity may reference a Person and RMA but ...
4
votes
1
answer
133
views
Tracking of changes in owned types in EF Core with property that has a predefined default value
On EF Core (>= 8, I think), I can define an entity with an owned entity, along with a simple DbContext like this
public class ObjectIn3DSpace
{
public int Id { get; set; }
public string ...
2
votes
1
answer
128
views
How can I mark parameter for a custom EF Core function to be encoded as column name
I want to use a json function of PostgreSQL, that is not part of NpgsqlJsonDbFunctionsExtensions
[DbFunction("jsonb_extract_path_text", IsBuiltIn = true)]
public static string JsonExtract(...
1
vote
0
answers
120
views
I get an error when I delete a user (cascade delete): "The DELETE statement conflicted with the REFERENCE constraint"
I use a peer-to-peer messaging system.
Here is the class:
public class MessagesModel
{
public long Id { get; set; }
public ApplicationUser UserExp { get; set; }
public ApplicationUser ...
4
votes
1
answer
265
views
Why does my dotnet ef upgrade detect pending changes on the pipeline, but work locally? (.NET 10)
We just migrated from .NET 8 to .NET 10, and had several migrations fail because of pending changes. For most it was a matter of fixing the pending changes with new migrations, but for one project ...
0
votes
1
answer
62
views
EF Core database-first upgrade dilemma
I have a large database in a MIB. Developed using EF Core with a database-first (or schema-first) approach.
When the system is fired up, it will first check the database version (a table in the ...
Advice
0
votes
2
replies
104
views
Why do integration tests remove IDbContextOptionsConfiguration<TContext> instead of the DbContext itself
When writing EF Core integration tests, I often see examples that remove the existing DbContext registration like this:
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
{...
3
votes
1
answer
135
views
How do I change column names for complex properties?
I have the following entity:
public class CPU
{
public int Id { get; set; }
public int Article { get; set; }
public int Garanty { get; set; }
public int ServiceLife { get; set; }
...
0
votes
1
answer
181
views
Error creating query string: Expression '@list' in the SQL tree does not have a type mapping assigned
Any Idea what I am making wrong or is this just a bug in the Library?
I am using MySql.EntityFrameworkCore version 10.0.0-rc and am trying to execute the following query:
var list = new List<string&...
-2
votes
1
answer
93
views
ASP.NET Core migrating database issue
I am new to ASP.NET Core and C# development, I was following a tutorial that create a web applications and get this error:
Unable to create a 'DbContext' of type 'AppDbContext'. The exception 'Unable ...
3
votes
1
answer
112
views
CREATE DATABASE permission denied in database 'master'.... migrating a database with EF Core
I'm writing a web application that should use EF Core to handle a SQL Server Express server database (the application is called RRDA).
The solution is divided into 3 projects:
RRDA.Web (the web ...
Tooling
0
votes
2
replies
64
views
EF Core: How to dynamically use an entity by table name (string) for data synchronization?
I'm building a C# program using EF Core that synchronizes data between a production and a test database.
The program retrieves a list of table names from the database, and only those tables should be ...
2
votes
1
answer
153
views
Postgres 23505 duplicate key exception in EF Core AddOrUpdate (race condition on initial insert)
I am encountering a duplicate key value violates unique constraint error in my ASP.NET Core application, and while I understand why it happens, I am unsure about the best practice to handle it using ...
3
votes
2
answers
255
views
How to make reusable linq statements, which can be included as part of a where, select and even order by clause
Imagine the following classes (much simplified version of the real use case):
public class Driver
{
public Int32 Id { get; set; }
public String Name { get; set; }
public String Country { ...
Best practices
0
votes
2
replies
73
views
Implement a feature to Activate/Deactivate employee from two Entities : User Profile and User Status in ABP Framework
I have to implement a feature to Activate/ Deactivate employee from two Entities : UserProfile and UserStatus in Domain Layer of ABP Application. I want to know the field where I can implement the ...
4
votes
1
answer
321
views
Use a Sequence to generate an ID on insert with Oracle and EF Core
I am currently trying to insert an entity into one of my tables using EF Core.
The entity looks like this:
public partial class Foo
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
...
0
votes
0
answers
136
views
Microsoft.Extensions.Logging log level configuration not working
In appsettings.json
{
"ApplicationInsights": {
// https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core
"ConnectionString": "SECRET"
},
&...
2
votes
3
answers
117
views
How to add the as-if navigation property defined by the external parameter?
I have a Product model class like this:
public class Product
{
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public decimal CatalogPrice { ...
1
vote
0
answers
54
views
DbContext being auto added to DI container [duplicate]
I'm in the process of removing .NET Aspire from my application and whilst this has been a mostly painless process, I'm having no end of issues getting my DbContext working the way I think it's meant ...
1
vote
1
answer
85
views
Can auto-mapper transform the record into the single string value?
Having product-attributes relation (one to many)...
public class Product
{
public int Id {get; set; }
public string Code { get; set; }
public string Name { get; set; }
public List<...
Advice
1
vote
0
replies
60
views
ASP.NET Core OData : implementing custom aggregate method
What is recommended way using Microsoft.AspNetCore.OData to implement custom aggregate method?
Using a CustomAggregateMethodAnnotation class will execute it in memory, how can I achieve execution of ...