【问题标题】:Creating entity relationship with entityframework in .net , c#在.net,c#中使用entityframework创建实体关系
【发布时间】:2020-11-09 19:23:16
【问题描述】:

我想创建一个包含这两个实体类关系的数据库表。迁移后我看不到关系表,只有我的 db set 表。我将在下面分享代码。

这是一个实体,

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; }
    
           
        }

这是另一个

public class Project:IEntity
    {

        public string ProjectName { get; set; }
        public int ProjectId { get; set; }

        public int ServerId { get; set; }
        public virtual Server Server { 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; }


    }

【问题讨论】:

    标签: c# .net asp.net-core asp.net-mvc-3 model-view-controller


    【解决方案1】:

    如果您的实体具有一对多关系,则必须将 ICollection 项目列表添加到服务器:

    public class Servers:IEntity   
     { 
       [Key]
       public int ServerId { get; set; }
     
        ...
    
        public virtual ICollection<Projects> Projects{ get; set; }
      }
    

    然后以最简单的方式,您可以在 DbContext 中使用覆盖 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);
       }
    }
    

    【讨论】:

      【解决方案2】:

      一般情况下,当我们在Entity Framework或Entity Framework Core中配置一对一或一对多关系时,在数据库中,会在Table中生成一个外键,我们可以用它来查找相关实体,而不是生成连接表。但是如果我们配置多对多关系,它可以生成Joining Table。

      例如:

      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; }
      }
      

      表结构:

      有关在实体框架中配置关系的更多详细信息,请查看以下链接:

      Configure Many-to-Many Relationships in Entity Framework Core

      Relationships

      【讨论】:

        猜你喜欢
        • 2021-02-09
        • 1970-01-01
        • 2022-07-19
        • 2022-12-05
        • 2019-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-12
        相关资源
        最近更新 更多