locked
foreign key pointing to its own table RRS feed

  • Question

  • Ok, I have a weird legacy db project that I'm mapping to entity framework 4 ctp 4. Poco first.

    Problem: I have table Education with props/columns:

    EducationId (PK, FK, int, not null)
    EducationName (nvarchar, not null)

    ...

    DemanLevel (FK, int, null)
    DemandUnit (FK, int, null)

    DemandLevel and DemandUnit are foreign keys pointing to the primary key Education.EducationId.

    How do you map this in a code first poco class with its entityconfiguration class?

     

    Wednesday, November 24, 2010 3:19 PM

Answers

  • Hi,

    Your class and mapping would look something like this;

    public class Education
    {
      public int EducationId { get; set; }
      public string EducationName { get; set; }
    
      public int DemandLevel { get; set; }
      public Education DemandLevelReference { get; set; }
    
      public int DemandUnit { get; set; }
      public Education DemandUnitReference { get; set; }
    }
    
    public class EducationConfig : EntityConfiguration<Education>
    {
      public EducationConfig()
      {
        this.HasRequired(e => e.DemandLevelReference)
          .WithMany()
          .HasConstraint((e1, e2) => e1.DemandLevel == e2.EducationId);
    
        this.HasRequired(e => e.DemandUnitReference)
          .WithMany()
          .HasConstraint((e1, e2) => e1.DemandUnit == e2.EducationId);
      }
    }

     

    ~Rowan

    Monday, November 29, 2010 11:55 PM
    Moderator
  • Hi,

    The code I supplied was one:many but with a reference for the one and rather then the many, if you just have the many side then the code would look like this;

    this.HasMany(e => e.DemandUnitReference)
       .WithRequired()
       .HasConstraint((e1, e2) => e1.DemandUnit == e2.EducationId);
    

     

    ~Rowan

    Wednesday, December 1, 2010 12:00 AM
    Moderator

All replies

  • Hi,

    Your class and mapping would look something like this;

    public class Education
    {
      public int EducationId { get; set; }
      public string EducationName { get; set; }
    
      public int DemandLevel { get; set; }
      public Education DemandLevelReference { get; set; }
    
      public int DemandUnit { get; set; }
      public Education DemandUnitReference { get; set; }
    }
    
    public class EducationConfig : EntityConfiguration<Education>
    {
      public EducationConfig()
      {
        this.HasRequired(e => e.DemandLevelReference)
          .WithMany()
          .HasConstraint((e1, e2) => e1.DemandLevel == e2.EducationId);
    
        this.HasRequired(e => e.DemandUnitReference)
          .WithMany()
          .HasConstraint((e1, e2) => e1.DemandUnit == e2.EducationId);
      }
    }

     

    ~Rowan

    Monday, November 29, 2010 11:55 PM
    Moderator
  • Hello, Thanks for your answer.

    Just a quick follow up with a part that I forgot to mention.

    DemandUnit is on to many, e.g:

    public int? DemandUnit { get; set; }
    public virtual ICollection <Education> DemandUnitReference { get; set; }

    I never really figured out how to map HasConstraint() on these kinds of foreign keys relationship.
    Do I just omit the hasconstraint for DemandUnitReference?

     

    Per

     

    Tuesday, November 30, 2010 2:45 PM
  • Hi,

    The code I supplied was one:many but with a reference for the one and rather then the many, if you just have the many side then the code would look like this;

    this.HasMany(e => e.DemandUnitReference)
       .WithRequired()
       .HasConstraint((e1, e2) => e1.DemandUnit == e2.EducationId);
    

     

    ~Rowan

    Wednesday, December 1, 2010 12:00 AM
    Moderator