Storing additional Set(s) of information for a many-to-many relationship using ASP.NET MVC

て烟熏妆下的殇ゞ 提交于 2019-12-12 05:13:01

问题


Storing additional information for a Many-to-Many relationship

I have the following two entities in a many-to-many relationship with the following model -

Disease

public class Disease
    {
        public Disease() 
        {
            this.Indications = new HashSet<App.Models.Indication.Indication>();
            this.LaboratoryParameters = new HashSet<App.Models.LaboratoryParameter.LaboratoryParameter>();
            this.Medicines = new HashSet<App.Models.Medicines.Medicine>();
        }

        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public Int32 DiseaseID { get; set; }

        [Required]
        [Display(Name = "Disease")]
        [StringLength(100)]
        public string DiseaseName { get; set; }

        public virtual ICollection<App.Models.Indication.Indication> Indications { get; set; }
        public virtual ICollection<App.Models.LaboratoryParameter.LaboratoryParameter> LaboratoryParameters { get; set; }
        public virtual ICollection<App.Models.Medicines.Medicine> Medicines { get; set; }       
    }

Medicine

public class Medicine
{
    public Medicine()
    {
        this.Diseases = new HashSet<App.Models.Disease.Disease>();
    }

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Int32 MedicineID { get; set; }

    [Required]
    [StringLength(100)]
    [Display(Name = "Generic Name")]
    public string Medicine_Generic_Name { get; set; }

    [Required]
    [StringLength(100)]
    [Display(Name = "Trade Name")]
    public string Medicine_Trade_Name { get; set; }

    [Required]
    [StringLength(15)]
    [Display(Name = "Availability")]
    public string Medicine_Availability { get; set; }

    [Required]
    [Display(Name = "No.of times free")]
    public Int32 Medicine_TimesFree { get; set; }

    public virtual ICollection<App.Models.Disease.Disease> Diseases { get; set; }

}

I want to store more information in the third table that connects the Disease and Medical entities. How do I do this especially since there is no model definition for this 'connection' table? Additionally, I need to mention that I want to store multiple sets of information for one association between a Disease and a Medicine. So, for eg.

For one combination of Disease and Medicine, there can be any number of records with the following additional information -

Potency, Torque, No.of pills, No.of times/day, Price of the dose ($)

Please suggest.


回答1:


You can map your 'join' entity into the model as well, and add any number of extra properties if you wish.

See below for a many to many example.


Many-to-many: custom join entity

I have to admit, I'm not really a fan of letting EF infer the join table wihtout a join entity. You cannot track extra information to a person-car association (let's say the date from which it is valid), because you can't modify the table.

Also, the CarId in the join table is part of the primary key, so if the family buys a new car, you have to first delete the old associations and add new ones. EF hides this from you, but this means that you have to do these two operations instead of a simple update (not to mention that frequent inserts/deletes might lead to index fragmentation — good thing there is an easy fix for that).

In this case what you can do is create a join entity that has a reference to both one specific car and one specific person. Basically you look at your many-to-many association as a combinations of two one-to-many associations:

public class PersonToCar
{
   public int PersonToCarId { get; set; }
   public int CarId { get; set; }
   public virtual Car Car { get; set; }
   public int PersonId { get; set; }
   public virtual Person Person { get; set; }
   public DateTime ValidFrom { get; set; }
}

public class Person
{
  public int PersonId { get; set; }
  public string Name { get; set; }
  public virtual ICollection<PersonToCar> CarOwnerShips { get; set; }
}

public class Car
{
  public int CarId { get; set; }
  public string LicensePlate { get; set; }        
  public virtual ICollection<PersonToCar> Ownerships { get; set; }
}

public class MyDemoContext : DbContext
{
  public DbSet<Person> People { get; set; }
  public DbSet<Car> Cars { get; set; }
  public DbSet<PersonToCar> PersonToCars { get; set; }
}

This gives me much more control and it's a lot more flexible. I can now add custom data to the association and every association has its own primary key, so I can update the car or the owner reference in them.

Note that this really is just a combination of two one-to-many relationships, so you can use all the configuration options discussed in the previous examples.



来源:https://stackoverflow.com/questions/42853304/storing-additional-sets-of-information-for-a-many-to-many-relationship-using-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!