Entity Framework many relationship x many extra field

Asked

Viewed 1,195 times

6

Good morning,

I’m studying the Entity Framework Code First and I’m wondering how I should create an Nxn model with extra information in the relationship table. For example:

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

Tabelas

So far so good, I create the model Person and Course and establish the relationship between them with Icollection and "saying" that a Course has several Person and vice versa and the EF creates this relationship (Courseinstructo).

Now let’s assume that you need some extra information in the Courseinstructo table, for example Active or Datainicio. In the proposed form the EF creates the table automatically not allowing me to add extra field in the relationship, right?

How should I proceed with this case? You should create 3 models:

  • Person
  • Course
  • Courseinstructo

and establish Person and Course’s relationship with Courseinstructo? What would be the outline of this code and then the manipulation of these records (INSERT, UPDATE, DELETE)? Thank you.

3 answers

3


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

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

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

In their classes Course and Person you must 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

  • I did all this. I created the classes and relations as indicated. Each one with its Dbset in Context and its repository. It works by adding Person and Course but adding their error ratio. Courseinstructor.Person = Person(previously registered) and the same for Course and then save.

  • Hi @Renato, if this helped you find the solution to the problem. Remember to mark the post as a solution, to help other users with a similar issue in the future :)

  • I was able to identify the problem. Thank you. I would like to refer to a Lonk that also contributed. http://answall.com/questions/14829/relacionamento-muitos-para-muitos-entity-framework-6

0

Just as Felipe Oriani mentioned the creation 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; }

        }

This way creates a new table in the database, without you having to create a new class. makes it easy that every time you have any changes in class tabs, you don’t need to make the change in another class with the same information.

0

Try it this way

    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

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.