首页 文章

MVC实现多对多的关系

提问于
浏览
0

这是使用MVC .net框架和实体框架“数据库优先”方法完成的 . 两个表之间存在多对多的关系 . 它们通过第三个表连接,第三个表将key作为来自第一个表的id和来自第二个表的id组合在一起 .

public class ManyToManyTable
  {
    [Required]
    [Key, Column(Order=0)]
    public int firsttableid { get; set; }

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

    public int something { get; set; }

    [ForeignKey("firsttableid")]
    public virtual FirstTable firstTable { get; set; }

    [ForeignKey("secondtableid")]
    public virtual SecondTable secondTable { get; set; }
}

第一个和第二个表有一些id是主键 .

我想创建View和Controller方法,为这个ManyToManyTable启用主细节输入表单 . 这将在Master和SecondTble中具有FirstTable的详细信息,并且当按下按钮Save时,所有这些都将保存在ManyToManyTable中 .

当然,First和Second Table都有这个属性:

public virtual ICollection<ManyToManyTable> ManyToManyTables { get; set; }

实现像这样的案例最简单的方法是什么?谢谢!

1 回答

  • 0

    EF具有多对多关系的默认约定 . 无需创建具体的
    映射类 . 您必须在"FirstTable"和"SecondTable" Class中包含导航属性,如下所示 .

    public class FirstTable
    {
        public FirstTable()
        {
            secondTableProperties = new HashSet<SecondTable>();
        }
        public int Id { get; set; }
        public int MyProperty2 { get; set; }
        public int MyProperty3 { get; set; }
        public virtual ICollection<SecondTable> secondTableProperties  { get; set; }
    }
    
    public class SecondTable
    {
    public SecondTable()
    {
            FirstTableProperties = new HashSet<FirstTable>();
    }
    public int Id { get; set; }
    public int MyProperty2 { get; set; }
    public int MyProperty3 { get; set; }
    public virtual ICollection<FirstTable> FirstTableProperties { get; set; }
    }
    

    从DBContext中删除映射类,仅包括上面两个类 . 构建并运行应用程序,EF将自动在SQL Server中创建Mapping表 . 通常,映射表仅包含其他两个表的主键 .

    您可以使用Fluent API对创建的映射表进行一些控制

    modelBuilder.Entity<FirstTable>()
                .HasMany<SecondTable>(s => s.FirstTableProperties)
                .WithMany(c => c.secondTableProperties)
                .Map(cs =>
                        {
                            cs.MapLeftKey("FirstTableId");
                            cs.MapRightKey("SecondTableId");
                            cs.ToTable("ManyToManyTable");
                        });
    

    如果您想使用具有其他属性的连接表,则上述多对多关系将无效 . 在这种情况下,您将必须创建两个一对多关系,如下所示 .

    public class FirstTable
    {
        public int Id { get; set; }
        public int MyProperty2 { get; set; }
        public virtual ICollection<ManyToManyTable> manytomany { get; set; }
    }
    
    public class SecondTable
    {
    public int Id { get; set; }
    public int MyProperty2 { get; set; }
    public virtual ICollection<ManyToManyTable> manytomany { get; set; }
    }
    
    public ManyToManyTable
    {
    [Required]
    [Key, Column(Order=0)]
    public int firsttableid { get; set; }
    [Required]
    [Key, Column(Order=1)]
    public int secondtableid { get; set; }
    public int AdditionalProperty { get; set; }
    public virtual FirstTable first { get; set; }
    public virtual SecondTable Second { get; set; }
    }
    

相关问题