How to resolve the update problem tracking instance in many-to-many in EFCore 7.0.10

本文讲述了在EntityFrameworkCore中,如何使用join-table处理两个类之间的关系,以及在数据库迁移和操作中遇到的问题,特别是关于外键约束和实体跟踪的处理方法。

Introduction

There is a scene that 2 classes are related by a join-table-like class. For example:

A class defines some basic properties, one of which is a set of Project, which means an object may refers to many Project

/*AssemblyLanguage.cs*/
public class AssemblyLanguage{
   public int ID {get;set;}
   public string Mnemonic {get;set;}
   public List<Project> Projects {get;set;}
}

/*Project.cs*/
   public class Project
   {
       public int ID { get; set; }
       
       public string Name { get; set; }
       
       public int ParentID { get; set; }

       /*-----self-reference, not the point in the text----*/
       public Project Parent { get; set; }
   	public List<Project> Children { get; set; }
       /*-------------------------------------------------*/
       public List<AssemblyLanguage> Nemonics { get; set; }
   }

The definition of DbContext, in which Entity Framework Core migrates and manipulates data from objects to database, is below.

public class AssemblyLangTranslatorDbContext : DbContext
{
        private string? DbPath = string.Empty;

        public AssemblyLangTranslatorDbContext()
        {
            DbPath = AppConfigurtaionServices.Configuration.GetSection("ConnectionStrings").GetSection("AssemblyLangTranslatorContext").Value;
        }


        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            options.UseSqlServer(DbPath);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Project>().Property(p=&g
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值