3

I need to define Organizational chart schema in Entity Framework.

PersonelJob Entity model is:

public class PersonelJob : BaseEntity
{
    public Int64 ID { get; set; }
    public string Name { get; set; }
    public Int64? ParentId { get; set; }
    public virtual PersonelJob Parent { get; set; }
    public virtual ICollection<PersonelJob> Childs { get; set; }
}

As you can see each job could be a job parent and have some job children.

How could map this entity to Database, with Fulent Api?

1 Answer 1

1

Override the OnModelCreating method on your context and add this configuration:

modelBuilder.Entity<PersonelJob>()
            .HasOptional(pj => pj.Parent)
            .WithMany(pj=>pj.Childs)
            .HasForeignKey(pj => pj.ParentId);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.