Home > Mobile >  Creating entity relationship with entityframework in .net , c#
Creating entity relationship with entityframework in .net , c#

Time:09-18

I want to create a database table which includes the relations of this two entity class. After the migration i cannot see the relation tables, only my db set tables. I will share the code below.

This is one entity,

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

And this is the other

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

    }

and my Dbset class

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


    }

CodePudding user response:

if your entities has one to many relation you must add ICollection list of Projects to Servers:

public class Servers:IEntity   
 { 
   [Key]
   public int ServerId { get; set; }
 
    ...

    public virtual ICollection<Projects> Projects{ get; set; }
  }

Then in the simplest way you can config relations with override OnModelCreating in DbContext:

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

CodePudding user response:

Generally, when we configure One-to-One or One-to-Many relationships in the Entity Framework or Entity Framework Core, in the database, it will generate a foreign key in the Table, and we could use it to find the related entities, instead of generating the Joining Table. But if we configure the Many-to-Many relationships, it could generate the Joining Table.

For example:

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

The Table structure:

enter image description here

More detail information about Configure relationships in the Entity Framework, please check the following links:

Configure Many-to-Many Relationships in Entity Framework Core

Relationships

  • Related