I am trying to figure out how to configure EF Code First, to generate a table. The problem I am running into is I am not sure how to correctly configure the table. The table has a parent-child relationship to itself. I would prefer a solution that uses FluentAPI configuration rather than attributes.
EF is generating the following table:
Specification
-------------
SpecificationId
Name
ParentSpecificationId
ParentSpecification_SpecificationId
Here is the class:
public class Specification
{
public Specification()
{
Children = new Collection<Specification>();
}
public int SpecificationId { get; set; }
public string Name { get; set; }
public int? ParentSpecificationId { get; set;}
public virtual Specification ParentSpecification { get; set;}
public virtual ICollection<Specification> Children { get; set;}
}
I tried the configuration below based on this question
public class SpecificationConfiguration : EntityTypeConfiguration<Specification>
{
public SpecificationConfiguration()
{
ToTable("Specification");
HasKey(k => k.SpecificationId);
Property(p => p.SpecificationId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasRequired(t => t.ParentSpecification)
.WithMany(t => t.Children)
.HasForeignKey(t => t.ParentSpecificationId)
.WillCascadeOnDelete(false);
HasOptional(t => t.Children)
.WithMany()
.HasForeignKey(t => t.SpecificationId)
.WillCascadeOnDelete(false);
}
}
What is wrong in my configuration and/or how do I get EF to stop generating the ParentSpecification_SpecificationId and put the value in the ParentSpecificationId?
Note: EF is putting the correct value in the column it is generating.