Entity FrameWork relationship many x many extra field

6

Good morning,

I am studying the FrameWork Entity Code First and am wondering how I should create a NxN template with extra information in the relationship table. For example:

Let's assume the following relationship (image taken from the MSDN website)

Sofarsogood,IcreatethePersonandCoursetemplateandestablishtherelationshipbetweenthemwiththeICollectionand"telling" that a Course has several Person and vice versa and EF creates this relationship (CourseInstructo).

Now let's assume you need some extra information in the CourseInstruct table, for example Active or StartDate. In the proposed way the EF creates the table automatically not allowing me to add extra field in the relationship, right?

How should I proceed in this case? You should create 3 templates:

  • Person
  • Course
  • CourseInstructo

and establish the relationship of Person and Course with CourseInstructo? How would the sketch of this code and then the manipulation of these records (INSERT, UPDATE, DELETE) be? Thank you.

    
asked by anonymous 10.09.2014 / 15:48

3 answers

3

In this case, you need a type to set this extra value, making it impossible for you to have direct access from your Person or Course class.

You can treat this relationship as a new Entidade de Agregação , since this information will be relevant to the relationship.

However, you must perform this entity's manual handling, as well as add a new DbSet<CourseInstructor> in its context.

In your classes Course and Person you should contain a list of CourseInstructor .

For example:

public class CourseInstructor
{
    public int Id { get; set; }

    public int CourseId { get; set; }

    public virtual Course Course { get; set; }

    public int PersonId { get; set; }

    public virtual Person Person { get; set; }

    public string Extra { get; set; }

    public int InstructorId { get; set; }

    public virtual Instructor Instructor { get; set; } 
}

A link that can also help you: Many Relationship For Many Entity Framework 6

    
10.09.2014 / 16:25
0

Just as Felipe Oriani mentioned the child of a new class "CourseInstructor", but the Entity FrameWork Code First does this automatically I will give an example

        public class Person 
        {
            public Person ()
            {
                this.Courses = new HashSet<Course>();
            }

            [Key]
     [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int PersonId{ get; set; }
public strng lastName{ get; set; }
public strig firstName{ get; set; }



            public virtual ICollection<Course> Courses { get; set; }

        }

///

      public class Course 
        {
            public Course ()
            {
                this.Persons = new HashSet<Person>();
            }

            [Key]
     [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int CouserId{ get; set; }
public string Title{ get; set; }
//...


            public virtual ICollection<Person> Persons { get; set; }

        }

In this way you create a new table in the database, without having to create a new class. makes it easy for you to change any other class with the same information every time you change any of the class's tabs.

    
07.07.2016 / 16:43
0

Try this out

    public class Course 
{

     [key]
     public int CourseId {get;set;}

     etc ....

     [ForeignKey("CourseID")]
     public virtual ICollection<Person> Persons { get;  set; }

}


public class Person 
{
    [key]
    public int PersonId {get;set;}

    etc...

    [ForeignKey("PersonID")]
    public virtual ICollection<Course> Courses { get;  set; }
}


public class CourseInstructo 
{
   [key] // de preferencia com autoincremento
   public int CourseInstructoId {get;set;}


   [ForeignKey("PersonId")]
   public virtual Person Person { get; set; }

   [ForeignKey("CourseId")]
   public virtual Course Course { get; set; }

   [Key, Column(Order = 0)]
   public virtual int PersonId { get; set; }

   [Key, Column(Order = 1)]
   public virtual int CourseId { get; set; }

   //Campo extra

}
    
07.07.2016 / 17:12