C# 在.net、c中创建与entityframework的实体关系#
我想创建一个数据库表,其中包含这两个实体类的关系。迁移后我看不到关系表,只有我的db set表。我将分享下面的代码 这是一个实体,C# 在.net、c中创建与entityframework的实体关系#,c#,.net,asp.net-core,asp.net-mvc-3,model-view-controller,C#,.net,Asp.net Core,Asp.net Mvc 3,Model View Controller,我想创建一个数据库表,其中包含这两个实体类的关系。迁移后我看不到关系表,只有我的db set表。我将分享下面的代码 这是一个实体, public class Server:IEntity { [Key] public int ServerId { get; set; } public string ServerPassword { get; set; } // publ
public class Server:IEntity
{
[Key]
public int ServerId { get; set; }
public string ServerPassword { get; set; }
// public List<AppUser> UserId { get; set; } do not necessary
public string ServerName { get; set; }
public DateTime ModifiedDate { get; set; }
public DateTime CreateDate { get; set; }
}
还有我的Dbset课程
public class AppIdentityDbContext : IdentityDbContext<AppUser, AppRole, string>
{
public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options) : base(options)
{
}
public DbSet<Server> Servers { get; set; }
public DbSet<Project> Projects { get; set; }
}
公共类AppIdentityDbContext:IdentityDbContext
{
公共AppIdentityDbContext(DbContextOptions选项):基本(选项)
{
}
公共数据库集服务器{get;set;}
公共数据库集项目{get;set;}
}
如果您的实体具有一对多关系,则必须将项目的ICollection列表添加到服务器:
public class Servers:IEntity
{
[Key]
public int ServerId { get; set; }
...
public virtual ICollection<Projects> Projects{ get; set; }
}
公共类服务器:IEntity
{
[关键]
public int ServerId{get;set;}
...
公共虚拟ICollection项目{get;set;}
}
然后以最简单的方式在DbContext中使用override OnModelCreating配置关系:
public class MyContext : DbContext
{
public DbSet<Servers> Servers { get; set; }
public DbSet<Projects> Projects { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Projects>()
.HasOne(p => p.Server)
.WithMany(b => b.Projects);
}
}
公共类MyContext:DbContext
{
公共数据库集服务器{get;set;}
公共数据库集项目{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasOne(p=>p.Server)
.有许多(b=>b.项目);
}
}
通常,当我们在实体框架或实体框架核心中配置一对一或一对多关系时,在数据库中,它将在表中生成一个外键,我们可以使用它来查找相关实体,而不是生成联接表。但是如果我们配置多对多关系,它可能会生成连接表
例如:
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public IList<StudentCourse> StudentCourses { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public string Description { get; set; }
public IList<StudentCourse> StudentCourses { get; set; }
}
public class StudentCourse
{
public int StudentId { get; set; }
public Student Student { get; set; }
public int CourseId { get; set; }
public Course Course { get; set; }
}
公共班级学生
{
公共int StudentId{get;set;}
公共字符串名称{get;set;}
公共IList学生课程{get;set;}
}
公共课
{
public int CourseId{get;set;}
公共字符串CourseName{get;set;}
公共字符串说明{get;set;}
公共IList学生课程{get;set;}
}
公共课学生课程
{
公共int StudentId{get;set;}
公立学生学生{get;set;}
public int CourseId{get;set;}
公共课程{get;set;}
}
表格结构:
有关在实体框架中配置关系的更多详细信息,请查看以下链接:
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public IList<StudentCourse> StudentCourses { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public string Description { get; set; }
public IList<StudentCourse> StudentCourses { get; set; }
}
public class StudentCourse
{
public int StudentId { get; set; }
public Student Student { get; set; }
public int CourseId { get; set; }
public Course Course { get; set; }
}