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

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

被折叠的 条评论
为什么被折叠?



