使用ASP.NET MVC为多对多关系存储其他信息集 [英] Storing additional Set(s) of information for a many-to-many relationship using ASP.NET MVC

查看:181
本文介绍了使用ASP.NET MVC为多对多关系存储其他信息集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存储多对多关系的其他信息

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 -

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; }       
    }

医学

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 ($)

请提出建议.

推荐答案

您也可以将"join"实体映射到模型中,并根据需要添加任意数量的额外属性.

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.

多对多:自定义加入实体

我必须承认,我并不是真正让EF推断没有连接实体的连接表的狂热者.您无法跟踪与人车关联的其他信息(例如,该信息的有效日期),因为您无法修改表格.

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.

此外,联接表中的CarId是主键的一部分,因此,如果家庭购买了一辆新车,则必须首先删除旧的关联并添加新的关联. EF对您隐藏了此内容,但这意味着您必须执行这两个操作,而不是简单的更新(更不用说频繁的插入/删除操作可能会导致索引碎片化-一件好事

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.

这篇关于使用ASP.NET MVC为多对多关系存储其他信息集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆