As of May 31, 2023, we have updated our Code of Conduct.

Questions tagged [ef-code-first]

EF Code-First is a way of using Microsoft's Entity Framework with POCO classes, as opposed to model-first or DB-first.

Filter by
Sorted by
Tagged with
-1 votes
0 answers
20 views

Add a modelBuilder for a circular relationship ( EF Core)

I have a database model. enter image description here Here is the configuration I have made: modelBuilder.Entity<ClassA>() .HasMany(p => p.ValuesClassB) .WithMany(p => p....
Faycel Zouaoui's user avatar
0 votes
0 answers
25 views

EF savechanges exception

I have this exception when I try to update my entites.. An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will ...
Luca Pannozzo's user avatar
0 votes
1 answer
127 views

Entity Framework - Db design suggestion

I have two models with many-to-many relationship: public class Person { public int PersonId { get; set; } public string Name { get; set;} public virtual ICollection<Hobby> Hobbies { ...
Eliya Amanoeel's user avatar
0 votes
1 answer
35 views

How do I update the join table in EFCore6 Code-First for Many-to-many relationships?

I am trying to create a School Management System using .NET WPF Application. My database is build with EFCore6, Code-First. I have two classes and there is a Many-to-Many relationship between them. `...
vld's user avatar
  • 1
0 votes
1 answer
43 views

Unable to add EF Core migration using composite key

I have the following entities: public sealed class CanonicalMake : EntityBase<MakeId> { private CanonicalMake() { } public MakeId MakeId { get; private init; } protected override ...
Harindaka's user avatar
  • 4,568
0 votes
0 answers
25 views

Entity Framework Int Data Not Getting Saved

I am working on .net core 7.0 api project with entity framework core, code first migrations. In this I have used Mediator Design Pattern. I have defined some entities which are inheriting some ...
Rahul Parab's user avatar
0 votes
0 answers
36 views

How user access should be validated in Api with EF Core

I have a API with 3 layers: Admin Controller and EditorViewerController Manager - which maps controller's DTO to a database model Repository - which puts data in the database In my API I have ...
Hantick's user avatar
  • 63
0 votes
0 answers
53 views

The property <PropertyName> could not be mapped because it is of type 'object', which is not a supported primitive type or a valid entity type

Hello I want to send userClaims to database and the property type is object,When in package manager console I run the : add-migration command it's give me this error :The property 'ServiceLog.Claims' ...
PEDRAM POURHAKIM's user avatar
0 votes
1 answer
65 views

EF Core 7 one-to-one relation, two foreign keys to the same table

I'm struggling with a problem of 2 foreign keys from the same table: public class Person { public int PId { get; set; } public string Name { get; set; } public int? HomeAddressId { get; ...
Andreas_k's user avatar
  • 103
0 votes
0 answers
16 views

EF Core Database Update Failed Foreign Key Constraint

I'm attempting to add a table (StatusList) to our database and configure the relationships for it between two other entities using entity framework. After creating the migration, when I update the ...
Lauren Dooley's user avatar
0 votes
2 answers
106 views

Blazor WASM SqlException: Cannot insert explicit value for identity column in table 'Categories' when IDENTITY_INSERT is set to OFF

Sorry for posting what might seem a duplicate but please bear with me! My first time working on a Blazor .NET Core applications with Entity Framework. My project is based of Thomas Beck's project from ...
Eliya Amanoeel's user avatar
0 votes
0 answers
16 views

How to seed data with autoincremented ID with EF Core Code First in Oracle DB

I have a problem with autogenerating primary key values when using Oracle DB with EF Core and db seed. Example entity: public class State : IBaseEntity, ILastEdited { [Key, DatabaseGenerated(...
Lucas's user avatar
  • 83
0 votes
0 answers
34 views

The instance of entity type 'WorkStreamOption' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked

When I am using repository pattern . I got error The instance of entity type 'WorkStreamOption' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. ...
kajal sharma's user avatar
1 vote
1 answer
65 views

Is there a way in EF core to use a dbContext to generate migration script

Looking for a code first way of using a context to generate the sql script to migrate from one specific migration to another. I cant seem to find this online. something like context.Database....
Daniel Goldberg's user avatar
0 votes
1 answer
83 views

What is the problem while i am trying to run Asp Net BoilerPlate Project ABP

I have found out a video about first configurations and run the ABP project. I have applied instructions of this video step by step but there are some problems on running. For example video created ...
G. ÖZEL's user avatar
0 votes
2 answers
3k views

C# Code First there is already an object named xxx in the database when trying to Update-Database in PM Console

I deleted some tables thinking they would be recreated on running my application but they weren't and it said couldn't find table. So to rerun the migrations I've deleted them from the migration table....
JulyAnn Jackson's user avatar
0 votes
2 answers
55 views

POST and GET values from 3 Tables linked through many to many relationship with Code First

Html form in Visual Studio Code with multi selectable checkboxes : <form name="CarServ" id="myForm"> <div> <label>CarModel:</label&...
zack's user avatar
  • 39
0 votes
2 answers
109 views

How to get Created and Modified columns to be set automatically using EF Core7 and NPGSQL connector

I have a problem similar to Entity Framework 5: Using DatabaseGeneratedOption.Computed option. I want to have a creation and modification dates on every table in database. This is what I have now: ...
Luk164's user avatar
  • 643
0 votes
0 answers
41 views

EF Core, defining a unique index with a foreign key's foreign key

I have a Farmer entity, which has many Trees. Each Tree has many Apples. All these apples have names. I would like to define a uniqueness constraint so no two apples that a farmer owns have the same ...
user1800978's user avatar
0 votes
0 answers
45 views

EF Core 8 + MVVM - ObservableCollection<string> gives errors in TPC

I am using EF Core 8 Preview 1.2 and CommunityToolkit.Mvvm 8.1.0 in a WASDK 1.2/WinUI 3 C# desktop app project. Here are my models: public abstract partial class Person : ObservableObject { [...
user21311740's user avatar
0 votes
0 answers
57 views

syntax error at or near "GENERATED" with debezium/postgres container

I want to create app using debezium and kafka with dotnet core app. Kafka, postgres, zookeper and connector running on docker without problem. I tried to this coomand dotnet ef database update --...
Fatih 's user avatar
  • 91
0 votes
1 answer
164 views

EF code first with MariaDb Error - the right syntax to use near 'max) NOT NULL

I use Entity Code First (EF core ver.6) in a project. The migration work correctly on the sql server, but it doesn't work on the MariaDb. I used the Pomelo.EntityFrameworkCore.MySql provider by these ...
Arani's user avatar
  • 855
0 votes
2 answers
54 views

How do I go about creating this relationship in EF Core?

When I try Add-Migration, I get this error: Unable to determine the relationship represented by navigation 'Book.Authors' of type 'ICollection<Person>' What I am trying to do, each person could ...
noobie's user avatar
  • 391
0 votes
1 answer
38 views

When I do and don't need attributes in Entity Framework

I want to make sure I understand all this. It seems to work but I worry when all these settings occur via the code reading my code and I'm left hoping the magic works (if it matters, I am using SQL ...
David Thielen's user avatar
0 votes
0 answers
21 views

Reference connection string in EF code first migration code

I want to use the current connection string that was supplied to Update-Database via the -ConnectionString parameter in come custom code I am inserting into the DbMigration.Up() method. Is there a ...
Eric's user avatar
  • 39
0 votes
2 answers
220 views

Problem foreign key between tables in ASP.NET Core 6 MVC & EF Core

I have a problem in my new project, I work in ASP.NET Core 6 and Entity Framework Core. In my project I have 10 tables, the problem comes from the relation between all the tables. Last time I posted a ...
user95's user avatar
  • 11
0 votes
0 answers
20 views

How to implement shared table in ef core 6 code first?

I'm using ef core 6 code first pattern for data access in my application. there is one scenario in which I need to connect multiple tables to one table column, as an example please refer bellow tables....
Roshan's user avatar
  • 3,186
1 vote
0 answers
41 views

How to define SetNull option over tables in SQL properly?

I have two tables in my project. Categories and Products. Surely there's a one-to-many relationship. public class Category : BaseClass { [MaxLength(50)] [Required] public ...
Earlingh's user avatar
0 votes
0 answers
26 views

EF: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Cards_Sellers_SellerId"

The database was created with Code-First. I want to add a relationship between Seller and Card later, but I get the following error, how can I fix it? Since there is data in the database, I do not ...
cenk takas's user avatar
0 votes
2 answers
645 views

ASP.NET Core 7 Identity - Custom User, Role, and PK Type

as title suggests, I want to customize the user class, the role class, and the PK types in Identity. I believe I have managed to mostly do it as I can see all of the changes in database (User and Role ...
Mephisto's user avatar
  • 418
0 votes
1 answer
151 views

Updating DB and Migration Asp.net.core

CodeFirst Approach. I have a multi project solution. My Projects are structured by. AccessService, ModelService and the ProjectName for the razorpages and rest of the code. TestDb: I have an existing ...
Rojhat's user avatar
  • 79
0 votes
0 answers
24 views

Navigation properties between IdentityUser and Custom Tables using Blazor WASM Hosted model

I'm using Entity Framework with Microsoft Identity Client on a Blazor WASM project with a hosted API. Having 2 tables related by Id - Events and EventParticipants I want to build a page that will ...
Avalari's user avatar
  • 37
1 vote
0 answers
119 views

EF Core Migration error: "Object reference not set to an instance of an object" when migrating two entities

the problem is the next. I have two entities, one called Service and other Owner, if i migrate them separately the process build, and create migrations with no problem. But when i tried to create a ...
Blas Gilich's user avatar
0 votes
1 answer
24 views

Getting generated value from Identity Column in EF Core 7

I still get zero as response on my Edentity Column after SaveChangesAsync(). Here's my entity public class Person { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] ...
yam cantorna's user avatar
0 votes
3 answers
484 views

Adding Migrations not working with EF Core Code first approach - C#

I'm building an ASP.NET Web Api with simple CRUD operations and a Database which should be generated using EF Code First and Migrations. In terms of the structure of the project, I am using ASP.NET ...
fabian's user avatar
  • 23
2 votes
2 answers
200 views

Read appsettings.json from Main project in DBContext Class library

I have a question. Is it possible to read the appsettings.json File in the DbContext class of another class library? I tried to write an AppSettingsService.cs to read the content of appsettings.json ...
fabian's user avatar
  • 23
0 votes
0 answers
9 views

How to change a one-to-many relationship to a many-to-many relationship in Entity Framework Code First

I have two tables: Place, and MenuSection, that currently have a one-to-many relationship defined like so: public class Place { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Key] ...
Jimmy's user avatar
  • 2,051
0 votes
1 answer
48 views

Ef Core 7, how to get one latest message per customer with take and skip

I have below customer and customer message model. I want to get top n customers with latest messages and have only the latest message per customer included. I am using EfCore 7 on Postgres and my ...
VKR's user avatar
  • 655
0 votes
0 answers
43 views

EF6 specify order of tables in Migration

In my database I have a couple tables with multiple PKs, which are each FKs. Below is the C# definition of one such Entity: /// <summary> /// Represents a permission record in the client ...
thebugsdontwork's user avatar
0 votes
2 answers
305 views

Failed executing DbCommand ON DELETE NO ACTION

I am working on an ASP.NET Core project using code first I want to alter database design. I have a case were I need to add two foreign keys to the same table, even I used DeleteBehavior.Restrict, ...
Lighto Gamer's user avatar
0 votes
1 answer
28 views

Exception when using Update-Database using code first workflow

I have a Movie model and a Genre class as follows: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace ...
Joshua Mitchum's user avatar
0 votes
0 answers
20 views

how to add 2 foreign keys in a new table using entity framework core

I am trying to make a table that has 2 foreign keys referencing two other tables, what I have is an ItemTable and an OrderTable , I want to take the primary key of both tables and put them in ...
Firefly0027's user avatar
0 votes
1 answer
95 views

How to create stored procedure in EF Core using .NET 6

I have a class library project in .NET 6 where I am using EF Core. I am able to create tables using the code-first approach, but stuck on how to create stored procedures. I have seen a few articles on ...
niler's user avatar
  • 23
0 votes
0 answers
22 views

Why Huge Data Are not inserted or updated correctly from MVC5 to sql Server

I made a project in MVC5 using code first approach. All of my project work correctly but a little problem occurred some times I have a table in sql server which has several columns with one Boolean ...
Mehrab Shaida's user avatar
0 votes
1 answer
82 views

How to tell what user "Integrated Security" is running as?

Running on my dev machine, connected to the db on another machine with this connection string in Visual Studio: Data Source=dev-db,1433;Initial Catalog=MyDb;Persist Security Info=True;Integrated ...
NovaDev's user avatar
  • 2,685
0 votes
1 answer
19 views

how to connect Files table with other tables that might upload files?

I have 4 entities in my project: Project, Task, Comment, AttachedFile In first 3 entities the user can attach files to them i think it's one to many relationship, So I introduce navigation property in ...
baha 's user avatar
0 votes
1 answer
66 views

Getting same Current and Original value of change tracker modified state

Below is my override saveChanges Methed which calls SetChanges Method public override int SaveChanges(bool acceptAllChangesOnSuccess) { SetChanges(); OnBeforeSaving();...
Kartik Pareek's user avatar
0 votes
0 answers
62 views

ASP.NET Core 6 / Entity Framework Core: relationships don't work

I can't deal with this problem and I hope you can help me. I have two models User and Friendship. In Friendship if I relate to RequestUser or ReceiveUser, I get the correct data. But ...
Mortimen's user avatar
-1 votes
1 answer
32 views

How to use general picture model relational database in C# with Entity Framework?

I want the use a general Picture model with an enum type - like this: public enum PictureType { Message = 0, User = 1, Mentor = 2, Group = 3, Tier = 4 } And I use this enum in ...
yunus's user avatar
  • 3
0 votes
1 answer
332 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
2 3 4 5
171