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.
8,501
questions
-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....
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 ...
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 { ...
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.
`...
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 ...
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 ...
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 ...
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' ...
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; ...
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 ...
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 ...
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(...
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. ...
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....
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 ...
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....
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&...
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:
...
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 ...
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
{
[...
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 --...
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 ...
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 ...
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 ...
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 ...
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 ...
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....
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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)]
...
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 ...
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 ...
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]
...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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();...
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 ...
-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 ...
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; ...