Asp.net mvc 多对多关系中的代码优先外键配置

Asp.net mvc 多对多关系中的代码优先外键配置,asp.net-mvc,asp.net-mvc-3,entity-framework,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 3,Entity Framework,Asp.net Mvc 4,我已经创建了两个表。 表1:包含ParentId和ParentName字段的父表。在此表中,ParentId是主键。 表2:包含ChildId和ChildName字段的子表。在此表中,ChildId是主键。 我想将ParentId和ChildId放入另一个表中。因此,我创建了名为MappingParentChild的表,其中ParentId为1,ChildId为1。我想将ParentId为1,ChildId为1作为外键。 我怎样才能做到这一点 public class Parent { publ

我已经创建了两个表。
表1:包含ParentId和ParentName字段的父表。在此表中,ParentId是主键。
表2:包含ChildId和ChildName字段的子表。在此表中,ChildId是主键。
我想将ParentId和ChildId放入另一个表中。因此,我创建了名为MappingParentChild的表,其中ParentId为1,ChildId为1。我想将ParentId为1,ChildId为1作为外键。 我怎样才能做到这一点

public class Parent
{
public int ParentId { get; set; } //Primary key
public string ParentName { get; set; }
}
public class Child
{
public int ChildId { get; set; } //Primary key
public string ChildName { get; set; }
}
public class MappingParentChild
{
public int Id { get; set; }
public int ParentId1 { get; set; } // want Foreign key for Parent(ParentId)
public int ChildId1 { get; set; }// want Foreign key for Child(ChildId)
}
public class MappingParentChildConfiguration :EntityTypeConfiguration<MappingParentChild>
{
public MappingParentChildConfiguration()
{
ToTable("MappingParentChild");
Property(m => m.Id).IsRequired();
Property(m => m.ParentId1).IsRequired();
Property(m => m.ChildId1).IsRequired();
}
}
公共类父类
{
public int ParentId{get;set;}//主键
公共字符串ParentName{get;set;}
}
公营儿童
{
public int ChildId{get;set;}//主键
公共字符串ChildName{get;set;}
}
公共类映射ParentChild
{
公共int Id{get;set;}
public int ParentId 1{get;set;}//需要父项的外键(ParentId)
public int ChildId1{get;set;}//需要子级的外键(ChildId)
}
公共类映射ParentChildConfiguration:EntityTypeConfiguration
{
公共映射ParentChildConfiguration()
{
ToTable(“映射父/子”);
属性(m=>m.Id).IsRequired();
属性(m=>m.ParentId1).IsRequired();
属性(m=>m.ChildId1).IsRequired();
}
}
我怎样才能做到这一点。请帮助我。

公共类家长
public class Parent
{
public int ParentId { get; set; }
public string ParentName { get; set; }
public virtual ICollection<MappingParentChild> parent{ get; set; }
}

public class Child
{
public int ChildId { get; set; }
public string ChildName { get; set; }
public virtual ICollection<MappingParentChild> child { get; set; }
}

public class MappingParentChild
{
public int Id { get; set; }
public int ParentId1 { get; set; }
public int ChildId1 { get; set; }
public virtual Parent pt{ get; set; }
public virtual Child Ch{ get; set; }
}

public class MappingParentChildConfiguration : EntityTypeConfiguration<MappingParentChild>
{
public MappingParentChildConfiguration()
{
ToTable("MappingParentChild");
Property(m => m.Id).IsRequired();
HasRequired(m => m.pt).WithMany(e => e.parent).HasForeignKey(m => m.ParentId );
HasRequired(m => m.ch).WithMany(e => e.child ).HasForeignKey(m => m.ChildId );
}
}
{ public int ParentId{get;set;} 公共字符串ParentName{get;set;} 公共虚拟ICollection父对象{get;set;} } 公营儿童 { public int ChildId{get;set;} 公共字符串ChildName{get;set;} 公共虚拟ICollection子项{get;set;} } 公共类映射ParentChild { 公共int Id{get;set;} public int ParentId1{get;set;} public int ChildId1{get;set;} 公共虚拟父节点pt{get;set;} 公共虚拟子Ch{get;set;} } 公共类映射ParentChildConfiguration:EntityTypeConfiguration { 公共映射ParentChildConfiguration() { ToTable(“映射父/子”); 属性(m=>m.Id).IsRequired(); HasRequired(m=>m.pt).WithMany(e=>e.parent).HasForeignKey(m=>m.ParentId); HasRequired(m=>m.ch).WithMany(e=>e.child).HasForeignKey(m=>m.ChildId); } }
您正在使用实体框架吗。如果有,哪个版本?我想这是你标题中的EF。你创建
MappingParentChild
类的具体原因是什么?因为EF不需要它来创建多对多关系