Questions tagged [entity-framework-core]
For questions about Entity Framework Core (EF Core) for .NET Core. Please also add a version-specific [ef-core-xx] tag, when applicable. Note that Entity Framework for .NET Framework is an older, different product that fits the [entity-framework] tag.
entity-framework-core
22,746
questions
0
votes
0
answers
9
views
How do I specify a user secret in a .NET Core console app?
I've created a .NET 8 Console app. I have also created a user secret with a connection string to one of our SQL Server databases, that looks like this:
{ "TTConnectionString": {
"...
0
votes
0
answers
12
views
EF Core 8: table splitting
I'm trying to use table splitting with EF Core 8 and I'm having some issues with the mappings.
Here's what my types look like:
public class Employee
{
public int Id { get; set; }
public int ...
0
votes
1
answer
26
views
No artifact output from dotnet-ef migrations script in Devops Pipeline
I have a Devops pipeline which used to run under a windows-latest agent. We've recently moved it to ubuntu-latest for a variety of reasons. However since that change, the dotnet-ef tool has not been ...
1
vote
1
answer
19
views
The instance of entity type 'Transaction' cannot be tracked because another instance with the same key value for is already being tracked
I am performing a read operation from the XLS file and doing the insert if the data is not present in the table.
DB Operation:
foreach (var transaction in transactions)
{
if (!_dbContext....
0
votes
1
answer
23
views
How to Reference a User Entity from Another Microservice in ASP.NET Core?
In my microservices architecture using ASP.NET Core, I have two distinct microservices: Identity.API for user management and PersonalAccount.API for handling personal account-related functionalities. ...
1
vote
1
answer
29
views
.NET EF Core : how to correctly insert and update a model in a many to many relationship with a join table with a payload?
I am writing a .NET CRUD application with layered architecture with EF Core for ORM. I have two models (Players and Teams) stored in the database with a join table between them (Player in Team).
The ...
0
votes
2
answers
71
views
How do I create an interface for a conditional one-to-many relation?
I am creating an inventory app for electronic components. There is a package for each component which will change if their assemble technology is on SMD or DIP. Each SMD and DIP has its own options, ...
1
vote
2
answers
73
views
Transform DateTime field to provided format in where clause
I'm trying to integrate a search function in my datatable in Asp.Net Core. I load the content dynamically with a query to my MSSQL server. As the database table content will increase drastically over ...
0
votes
1
answer
41
views
EF Core 8 dbcontext not loading new data when another app is changing data
I have a Blazor app that accesses data using EF Core. When a separate app edits state on the data, my Blazor app is not getting the data correctly when I reload it.
It fails to load when I try to get ...
0
votes
0
answers
43
views
EF user-defined function mapping: "Ensure the parameter can be mapped by the current provider"
I have a custom type UniversityId (value type):
namespace TestEntityFramework;
public sealed class UniversityId(string value)
{
public string Value { get; } = value;
public override string ...
1
vote
0
answers
45
views
JsonApiDotNetCore and filtering using a udf
I am using JsonApiDotNetCore, .NET Core, and EF Core on SQL Server and creating a REST project implementing the Json API standard.
I have plenty of resources (models) but one of them has some fields ...
2
votes
1
answer
39
views
Entity property is set prior to SaveChangesAsync then sometimes is saved to the database as null, and changed to null in the entity
I have an intermittent problem. I have a C# API project that reads an entity from the database, updates its properties, then calls DbContext.SaveChangesAsync. The entity has a nullable int ...
-3
votes
0
answers
60
views
Why is my code not returning any records? Even though there are rows found. (Entity Framework Core)
using Binus.WS.Pattern.Entities;
using Microsoft.EntityFrameworkCore.Metadata;
using SSG5.C1.BSQWebAPI.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System....
0
votes
1
answer
26
views
Confused about NpgSql connection pooling vs AddDbContextPool
I'm trying to implement database pooling in my .net core application that's connecting to PostgreSql db. While reading documentation on NpgSql, It states that pooling is avaible in the this library (...
0
votes
1
answer
53
views
Many-to-many relationship update in EF Core
In my two classes I created a many-to-many relationship:
public class Author
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[DisplayName(&...
-1
votes
1
answer
56
views
Equals method never called by LINQ expression [duplicate]
My data class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using static iText.StyledXmlParser.Jsoup.Select.Evaluator;
...
0
votes
0
answers
56
views
Entity Framework: could not create socket for "db_connection": listen unix /tmp/cloudsql-proxy-tmp/db_connection/.s.PGSQL.5432: bind: invalid argument
I'm using Google Cloud Run to host a .Net Core 8 application using the Google Cloud Functions Framework. THe entity framework version is 8.0.7. The PostgreSQL database is hosted on Google Cloud SQL. ...
0
votes
1
answer
55
views
Issue with LINQ Query in Async Method: 'The LINQ expression could not be translated'
I have an asynchronous method in C# that retrieves a list of users with various nested includes and filters. The method works fine when I don't pass the arbsResponse parameter, but I encounter an ...
-1
votes
0
answers
64
views
Performance degradation of linq query in C# after .NET Core upgrade to 8 [duplicate]
I have the simple linq query in my .NET Core C# app. We have upgraded from .NET Core 3.1 to .NET 8.
My query is as follows:
Decimal[] cks = mem.Select(a => Convert.ToDecimal(a.KEKE_CK)).ToArray();
...
1
vote
2
answers
42
views
Unable to create an object of type 'IdentityDataContext'
I'm encountering an issue while trying to update my database using Entity Framework Core in my .NET project. When I run the command:
dotnet ef database update --context IdentityDataContext
I receive ...
0
votes
1
answer
49
views
dotNET minimal API getting 404 not found on a secure endpoint
I am trying to implement a rest api using the dotNET framework. I am experiencing a problem with the "/secure" endpoint, getting 404 as a response message whether I login or not.
If I don't ...
0
votes
0
answers
46
views
.Net Core 8 Entity Framework (MySQL) DROP PROCEDURE IF EXISTS `MYSQL_BEFORE_DROP_PRIMARY_KEY` Error
I'm created a migration to add a few tables and update one table (add one column). The database is MySQL 8.0.31 and the EF provider is Oracle's MySql.EntityFrameworkCore
The migration was completed ...
0
votes
1
answer
53
views
System.InvalidCastException: Can't cast database type timestamp without time zone to DateOnly
I get this error message when using EF Core to query data:
System.InvalidCastException: Can't cast database type timestamp without time zone to DateOnly
This is a sample code that causes an error ...
-1
votes
0
answers
51
views
Why where clause ignores data comparison? [closed]
I have such code:
var utcNow = DateTime.UtcNow;
Expression<Func<MyEntity, bool>> condition = entity => entity.StartedAt <= utcNow && entity.EndedAt >= utcNow;
var query = ...
0
votes
1
answer
38
views
Unable to perform migrations in .NET Core
The error which I am facing:
PM> update-database
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application ...
0
votes
0
answers
31
views
Storing tree structures where a node can be in multiple trees
I am trying to find a way to build a model in Entity Framework to represent Courses and Lessons, where each course can have multiple Courses and lessons within it. However i also want to make it so ...
0
votes
0
answers
53
views
Adding complex objects in .NET Core
These are my entities:
public class Order
{
[Key]
public long OrderId { get; set; }
public string CustomerId { get; set; }
[ForeignKey("CustomerID")]
[Required]
...
0
votes
0
answers
33
views
EF Core : how to make sure we get up to date data from a DbSet
I'm pretty new to EF Core and I'm a facing an issue where I get some entities that do not seem to exist in my database anymore.
Here is how I proceed:
var myEntity = _context.MyDbSet.Where(m => m....
0
votes
1
answer
34
views
EF Core customize scaffolding without deriving from internal classes
Entity Framework Core Customize Scaffolding describes a process for customizing the entities created when scaffolding an existing database using Entity Framework Core. This we've done successfully up ...
0
votes
1
answer
76
views
ModelBuilder Entity in ASP.NET Core MVC
In ASP.NET Core 6, I need to rename the database model of ASP.NET Core Identity. I do not use APIs, but just ASP.NET Core 6 MVC.
This my code:
protected override void OnModelCreating(ModelBuilder ...
0
votes
1
answer
45
views
Entity Framework Core error during scaffolding in .NET 8
I am trying to build an Azure Function app. To import my database context using EF Core, I am running the scaffolding command as shown below.
Scaffold-DbContext 'Data Source=XXXX;Initial Catalog=XXXX;...
0
votes
0
answers
33
views
Stop EF from populating navigation properties
I am trying to create an application that restores the entities for one environment by copying entities from another environment (direct database backup is not possible due to specific issues).
My ...
0
votes
1
answer
59
views
Entity Framework Core : Include not working for many-to-many relationship
I'm having trouble with .Include() not working for many-to-many relationships in Entity Framework Core. My setup works fine for one-to-many relationships, but the related entities are not being loaded ...
1
vote
3
answers
60
views
EF Core eager loading collections when GroupBy used after Include
var productsByRecalls = await _context
.Products
.Include(x => x.Suppliers) // <-----
.Include(x => x.Recalls) // <-----
...
0
votes
2
answers
47
views
EF Core 6 : can I set an entity value from a different table?
I am looking for a way to set up my context so that when it populates a particular property of a particular entity, it pulls the data from a different table. Something similar to this fake syntax (...
0
votes
1
answer
54
views
Application Insights sometimes does not show the query from a SQL call
I have a Blazor server application with 2 databases it calls via Entity Frameworks. One is the Identity Library DB. The other is my application DB.
When I go in to Application Insights and:
Click on ...
0
votes
0
answers
20
views
Use Entity Framework DI in Azure Durable functions is recommended [closed]
I want to use Entity Framework core in my Azure Durable function using dependency injection but problem is that the Durable function is marked as static class, so I am confused to make the class ...
1
vote
0
answers
35
views
Entity Framework Core in memory reading wrong data type
My functional tests use Microsoft.EntityFrameworkCore.InMemory version 7.0.20.
When I run the functional tests for any entity that has a dateonly field, I get an error:
Expression of type 'System....
1
vote
0
answers
29
views
Complex multi level EF Core 8 inheritance issue
Please consider the following:
DB Context:
// DbContext.cs
public partial class OperationsDbContext : DbContext
{
public virtual DbSet<AnimalBase> AnimalBases { get; set; }
public virtual ...
0
votes
0
answers
44
views
Unable to create a 'DbContext' of type '' whenever I try to add migration
The problem occurs whenever I try to add migration with any kind of DbSet. When I leave all of the dbsets commented the ApplicationDbContext instance exists. I have searched throughout all similar ...
0
votes
0
answers
56
views
Setting up and Querying Temporal Tables in .Net 7
Using .Net 7 Entity Framework, I have a system of tables that mark as Temporal, so that I can reconstruct the data structure from the past (i.e. when a user makes a change, I should be able to roll ...
0
votes
1
answer
26
views
Entity Framework Core one-to-zero-or-one not binding included relations when querying
I have an Entity Framework Core code-first database with a one-to-one-or-zero relationship set up like this:
public class PrimaryRow
{
public int Id { get; set; }
public string Value { get; ...
0
votes
1
answer
31
views
Program exit with code 0 on if (await db.Database.CanConnectAsync())
I have a C# Console App with the following Main (lightly trimmed):
public static void Main(string[] args)
{
Start(args);
}
private static async void Start(string[] args)
{
new ...
0
votes
2
answers
37
views
InvalidOperationException: Unable to resolve service for type '...Context' while attempting to activate
My project is being built with asp.net core razor pages and dotnet 7
I have considered a separate layer for the database and my database is built in the Database First method
The connection string is ...
-1
votes
0
answers
27
views
Load linked entity via automapper into dictionary
I'm trying to use automapper to pull in a relationship's collection and convert to a dictionary, but I'm struggling with syntax. I have these two entities:
class Station {
public int Id { get; set; ...
1
vote
1
answer
42
views
Expression Visitor access property on parameter representing local variable
Trying to write an ExpressionVisitor that would rewrite a query
Starting point
var contractSubjectKey = new ContractSubjectKey(subjectId, rank);
insurance = context.Set<Insurance>()
....
0
votes
1
answer
63
views
EF Core SaveChanges fails (v8.01 on .NET 8)
After upgrading from .NET Framework with EF to .NET 8 with EF Core, I am unable to modify my SalesQuotes.
I am using table per type strategy.
This code fails:
var connect = HandyDefaults....
0
votes
1
answer
24
views
EF Core - Need to reference the many-to-many join table in code-first application
I have a mapping application that will display pins on a map that represent Features at Member addresses (e.g. a Member lives at lat/long 'X' and is associated to Feature 'Y'). Members can be ...
0
votes
0
answers
21
views
Failing migration after update to EF Core 8: The object has been removed from the model
After upgrading our application from .NET 6 to .NET 8, we are able to access the application and interact with the DbContext. However when interacting with DbContext through the dotnet-ef tool, for ...
0
votes
2
answers
64
views
Moving database contexts to library project results in "FileNotFound" exception
I have a .NET Core 7 solution EIT which had only 1 project: EIT. This project uses Entity Framework Core to model and access the database. This worked fine.
Now I want to add several Windows services ...