Asp.net mvc 多对多关系的MVC实现

Asp.net mvc 多对多关系的MVC实现,asp.net-mvc,entity-framework,many-to-many,master-detail,Asp.net Mvc,Entity Framework,Many To Many,Master Detail,这是使用MVC.net框架和实体框架“数据库优先”方法完成的。两个表之间存在多对多关系。它们通过第三个表连接,该表将键组合为第一个表的id和第二个表的id public class ManyToManyTable { [Required] [Key, Column(Order=0)] public int firsttableid { get; set; } [Required] [Key, Column(Order=1)] public

这是使用MVC.net框架和实体框架“数据库优先”方法完成的。两个表之间存在多对多关系。它们通过第三个表连接,该表将键组合为第一个表的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,它是主键

我想创建视图和控制器方法,以启用此ManyToManyTable的主详细信息输入表单。在主表中有第一个表,在详细信息中有第二个表,当按下Save按钮时,所有这些都将保存在ManyToManyTable中

当然,第一个表和第二个表都具有以下属性:

public virtual ICollection<ManyToManyTable> ManyToManyTables { get; set; }
公共虚拟ICollection ManyToManyTables{get;set;}
实现这种情况最简单的方法是什么?
谢谢大家!

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

 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; }
}
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; }
} 
公共类第一个表
{
公共第一表()
{
secondTableProperties=new HashSet();
}
公共int Id{get;set;}
公共int MyProperty2{get;set;}
公共int MyProperty3{get;set;}
公共虚拟ICollection secondTableProperties{get;set;}
}
公共类第二表
{
公共第二表()
{
FirstTableProperties=new HashSet();
}
公共int Id{get;set;}
公共int MyProperty2{get;set;}
公共int MyProperty3{get;set;}
公共虚拟ICollection FirstTableProperties{get;set;}
}
从DBContext中删除映射类,仅包括以上两个类。构建并运行应用程序,EF将在SQL server中自动创建映射表。通常,映射表只包含其他两个表的主键

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

modelBuilder.Entity<FirstTable>()
            .HasMany<SecondTable>(s => s.FirstTableProperties)
            .WithMany(c => c.secondTableProperties)
            .Map(cs =>
                    {
                        cs.MapLeftKey("FirstTableId");
                        cs.MapRightKey("SecondTableId");
                        cs.ToTable("ManyToManyTable");
                    });
modelBuilder.Entity()
.HasMany(s=>s.FirstTableProperties)
.WithMany(c=>c.secondTableProperties)
.Map(cs=>
{
cs.MapLeftKey(“第一表ID”);
cs.MapRightKey(“第二个表ID”);
cs.ToTable(“ManyToManyTable”);
});
如果要使用具有其他属性的联接表,上述多对多关系将不起作用。在这种情况下,您必须创建两个一对多关系,如下所示

 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; }
}
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; }
} 
公共类第一个表
{
公共int Id{get;set;}
公共int MyProperty2{get;set;}
公共虚拟ICollection manytomany{get;set;}
}
公共类第二表
{
公共int Id{get;set;}
公共int MyProperty2{get;set;}
公共虚拟ICollection manytomany{get;set;}
}
公共多功能表
{
[必需]
[键,列(顺序=0)]
public int firsttableid{get;set;}
[必需]
[键,列(顺序=1)]
public int secondtableid{get;set;}
公共int附加属性{get;set;}
公共虚拟FirstTable first{get;set;}
公共虚拟第二个表第二个{get;set;}
} 

一旦开始添加与严格的多对多无关的列,即“public int something{get;set;}”。您正在创建一个新实体,它应该映射为1对多-多对1关系。我不清楚,表是这样实现的。我只是不知道如何创建视图和操作,例如,创建或更新,以主详细信息的形式,其中第一个表是主表,第二个是详细表。我希望您的MVC端和数据层端有单独的数据模型。上面的代码只显示了数据层中的实体。在MVC端,您可以创建一个单独的视图模型,该模型具有从用户获取详细信息所需的所有属性。单击“保存”按钮时,您必须验证视图模型并将其映射到相应的实体模型,并保存到数据库上下文中。FirstTable.Add(新的FirstTable{prop1=model.M1,prop2=model.M2});Add(新的第二个表{prop1=model.D1,prop2=model.D2});context.SaveChanges()谢谢您的建议。不,我没有把它们分开,似乎这是EF MVC combo中没有很好计划的事情。我曾经在甲骨文公司工作,那里的主细节可以在几秒钟内完成。因此,我很困惑在.NETMVC中实现同样的东西有多么困难。它需要这么多的助手。。。