23,537 questions
-1
votes
0
answers
25
views
Why does this C# code return a null reference exception without warning?
My understanding of C#'s null safety features (from C# 8.0 onwards) is that (possibly) null variables must be typed as nullable and that the compiler will statically detect any parts of the code which ...
1
vote
1
answer
81
views
How to conditionally compose the EF from parts?
I have two tables Products and CustomerProduct. I need to compose an Entity Framework Core object that returns the result based on arguments of the Web API endpoint method.
The method roughly works ...
0
votes
1
answer
94
views
How to have a repository-type table without FKs?
Is it possible to have a type in Entity Framework Core that doesn't create FKs to every other table that uses it? Creating the DB-Scheme from the C#-model with Add-Migration and Update-Database.
For ...
0
votes
2
answers
78
views
Defining many-to-many with join table foreign key names without concrete c# domain table (shadow join)
I am trying to link a many-to-many relationship without any intermediary class in my domain.
The link is SoundProfiles <-> Switches. They're both not required. The error I get is
One or more ...
1
vote
1
answer
92
views
Unable to create a 'DbContext' of type 'AppDbContext'. The exception 'Object reference not set to an instance of an object.' was thrown
I'm getting this error :
Unable to create a 'DbContext' of type 'AppDbContext'. The exception 'Object reference not set to an instance of an object.' was thrown while attempting to create an instance....
12
votes
5
answers
3k
views
installing dotnet-ef dotnet tool throws error
I’m trying to install the Entity Framework Core CLI tools globally using the .NET CLI command:
dotnet tool install --global dotnet-ef
However, the installation fails with the following error message:
...
Advice
2
votes
3
replies
110
views
Does the EF Core 8.0.20 process correctly AsSplitQuery queries with conditions & paging that pull data from tables with one-to-many relationships?
Do you know if EF Core correctly processes queries that apply filters and paging and select data from tables with one-to-many relationships when .AsSplitQuery() is used?
If you go here: https://learn....
1
vote
1
answer
157
views
.NET migrations with vulnerabilities
I am attempting to create a new EF Core migration using the add-migration command in the Visual Studio Package Manager, but the command is failing with the following error:
The running command ...
3
votes
1
answer
110
views
Self-contained publish works locally but fails on other machines: "Failed to update database to latest state"
I'm creating a self-contained publish of my ASP.NET Core application.
When I run it on my development machine, everything works correctly: the app can connect to the remote SQL Server and db.Database....
3
votes
3
answers
119
views
Delete rows in two tables with relations in both directions using Entity Framework Core
How do you delete rows in tables using Entity Framework (Core), when you have two tables that are referencing each other in both directions?
Consider there classes:
public class ImageSeries
{
int ...
2
votes
0
answers
99
views
NUMBER(1,0) mapped to bool instead of int causes conversion and mapping errors
I'm connecting to an Oracle database using EF Core.
When scaffolding the database (Scaffold-DbContext), EF Core automatically maps columns of type NUMBER(1,0) to bool.
To avoid this, I changed the ...
1
vote
2
answers
122
views
Foreign keys are null when retrieved, even though stored correctly in database
I have the following classes in my (code-first) Entity Framework Core database schema:
public class User
{
public int Id { get; set; }
// ... Irrelevant bits omitted
}
public class Church
{
...
1
vote
1
answer
171
views
Repository Interface to perform database bulk update with Entity Framework Core 8.0.6
I have the following interface signature that I want to use for a more efficient bulk update using EF Core 8.0.6
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using ...
3
votes
2
answers
121
views
Error when accessing data, EF Core seems to create its own column
I have a project that has uses a database-first approach. I have used db-scaffold to create the models and have also created 'custom' models to contain extra info and code.
I have a model class ...
1
vote
1
answer
113
views
Entity with compound primary key also create index on single column [duplicate]
I just added an EF entity called PurchaseOrderProduct, which creates a many-to-many link between my PurchaseOrders and Products.
[PrimaryKey(nameof(PurchaseOrderId), nameof(ProductId))]
public class ...
6
votes
3
answers
210
views
Preserve a foreign key using a uniqueness constraint while dropping a primary constraint
I have a SQL Server database I mostly interact with using EF Core in a .NET project: I'll analogize my domain logic for simplicity.
I have a Fruits table holding general fruit data in columns Weight, ...
0
votes
2
answers
82
views
ASP.NET Core Web API + EF: how to convert the list of entities to queryable?
I need to rewrite the code for getting and filtering the list of products. The older version used directly the SQL table Products, and filtered the result by code or name of a product, category, etc, ...
2
votes
1
answer
54
views
EF Core HasOne(...).WithOne() throws many InvalidException at migration
I am facing this problem at migration - here are my model classes:
// Principal (parent)
public class Blog
{
public int Id { get; set; }
}
// Dependent (child)
public class BlogHeader
{
...
1
vote
0
answers
227
views
Unable to create a 'DbContext' of type 'RuntimeType'
I get this error when migrating from Entity Framework:
dotnet ef migrations add initApp
Unable to create a 'DbContext' of type 'RuntimeType'. The exception 'Unable to resolve service for type '...
1
vote
1
answer
97
views
How to configure model with two principals related to the same child? [closed]
I've got a class model with two principal classes (PrincipalEntity1, PrincipalEntity2) which are both related to the same child entity (TwiceDependentEntity). PrincipalEntity2 is related directly to ...
0
votes
0
answers
123
views
How can I use Entity Framework Core's direct update with a property that has converter
When setting the value on the entity, it works as easily as assigning an empty collection to the property. The property has converters to-from JSON.
However, both throw exceptions:
await db....
0
votes
0
answers
41
views
EF Core 9 Scaffolding NPGSQL - attribute tags vs modelBuilder
When I ran a scaffold command on my NPGSQL db with the typical default format I find on the web:
Scaffold-DbContext <connection-string> OutputDir Models -Force
It created all my model classes ...
3
votes
1
answer
259
views
Safely rename table in EF Core
I'd like to rename a table in Entity Framework Core, code first. I do not want to drop and create the table, as this would lose all my data. As I search for information on this, I see that the vast ...
1
vote
0
answers
55
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
101
views
.NET Equals override not being called inside FirstOrDefaultAsync
I'm in a .NET 6 web application with EFCore, and I have these boilerplate abstract classes and interfaces for defining entities. I know it's quite a number of complexity layers, but it really helps me ...