cickness
cickness

Reputation: 907

How make One-to-Many relationship in Entity Framework Core

Who can tell how to make a model in this situations. One employee earlier (until today, there is a date of dismissal) could hold several positions, but now they can only hold one position (there is no date of dismissal).

public class Employee
{
    [Key]
    public int EmployeeId { get; set; }
    public Position Position { get; set; }
    public DateTime DateStart { get; set; }
    public DateTime DateQuit { get; set; }
}

public class Position
{
    [Key]
    public int PositionId { get; set; }
}

Do I need to add to the class Position next code?

public Employee Employee { get; set; }

Do I need add to Employee class next code?

public int PositionId { get; set; }

and іs it necessary to make a method OnModelCreating?

protected override void OnModelCreating(Modelbuilder modelBuilder)
{
    modelBuilder.Entity<Company>()
            .HasMany(c => c.Employees)
            .WithOne(e => e.Position);
}

Upvotes: 0

Views: 193

Answers (3)

TanvirArjel
TanvirArjel

Reputation: 32069

As you said, you are expecting one employee to have one position but one position can have multiple employees at a time. In a nutshell its one-to-many between Position and Employee.

So you can do as follows:

public class Employee
{
    [Key]
    public int EmployeeId { get; set; }
    public int PositionId { get; set; }
    public DateTime DateStart { get; set; }
    public DateTime DateQuit { get; set; }

    public Position Position { get; set; }
}

public class Position
{
    [Key]
    public int PositionId { get; set; }
}

Then in the OnModelCreating:

protected override void OnModelCreating(Modelbuilder modelBuilder)
{
    modelBuilder.Entity<Employee>()
            .HasOne(e => e.Position)
            .WithMany()
            .HasForeignKey(e => e.PositionId);
}

To learn more about EF Core relationships: EF Core Relationships

Upvotes: 0

zied ben othman
zied ben othman

Reputation: 82

add foreign key of Position table in Employee

   public class Employee
        {
            [Key]
            public int EmployeeId { get; set; }
            public Position Position { get; set; }
            public DateTime DateStart { get; set; }
            public DateTime DateQuit { get; set; }
            public int PositionId { get; set; }
            public virtual Position Position { get; set;}
        }

then add icollection of employees in position table

 public class Position
    {
        [Key]
        public int PositionId { get; set; }
        public virtual ICollection<Employee> Employees { get; set; }  

    }

Upvotes: 0

Edu
Edu

Reputation: 2643

  1. Do I need to add to the class Position next code?

First, it should be public ICollection<Employee> Employees { get; set; }.

Second, it is optional. If you ever want to know who are the employees for a certain position, you can add it.

  1. Do I need add to Employee class next code?

Yes. That is the foreign key that defines the relationship between the two entities.

  1. Is it necessary to make a method OnModelCreating?

Yes, because then the model is appropriately configured in EntityFramework.


If you want to have the employee list in the position entity, it should be something like:

modelBuilder.Entity<Position>()
    .HasMany<Employee>(g => g.Employees)
    .WithRequired(s => s.Position)
    .HasForeignKey<int>(s => s.PositionId);

or

modelBuilder.Entity<Employee>()
    .HasRequired<Position>(s => s.Position)
    .WithMany(g => g.Employees)
    .WithForeignKey<int>(s => s.PositionId);

If you don't need the employee list in the position entity, it should be something like:

modelBuilder.Entity<Employee>()
    .HasRequired<Position>(s => s.Position)
    .WithMany()
    .WithForeignKey<int>(s => s.PositionId);

Upvotes: 1

Related Questions