4

I'm building a hierarchical Database with "closure table" to build the tree It is a self referencing table, and the two keys should become the primary key.

The Problem is, I end up with 5 columns, when I expect only 3.

Here is what I tried:

    public class Tree
    {
        public int TaskId { get; set; }
        public Task Task { get; set; }  //navigation Property to TaskTable

        public int? ChildId { get; set; }
        public Tree Child { get; set; } //navigation Property

        public int Length { get; set; } //Length

    }

    public class Task
    {
        public int TaskId { get; set; }
        public virtual ICollection<Tree> Trees { get; set; }
    }

    modelBuilder
        .Entity<Task>()
        .HasKey(t => t.TaskId);

    modelBuilder
        .Entity<Tree>()
        .HasKey(a => new { a.TaskId, a.ChildId });

The result is a table with 5 columns:

  1. TaskId
  2. ChildId
  3. Length
  4. Child_TaskId
  5. Child_ChildId

I expected:

  1. TaskId
  2. ChildId
  3. Length

I'm guessing some fluent api missing, but I couldn't get to work otherwise ?

Wozilla
  • 61
  • 1
  • 8

0 Answers0