Entity framework 在EntityFramework6代码优先方法中,在两个表之间创建一对多和多对一映射

Entity framework 在EntityFramework6代码优先方法中,在两个表之间创建一对多和多对一映射,entity-framework,entity-framework-6,ef-code-first,ef-fluent-api,ef-code-first-mapping,Entity Framework,Entity Framework 6,Ef Code First,Ef Fluent Api,Ef Code First Mapping,我想使用EntityFramework6代码优先的方法创建以下两个表。我可以使用属性表示法或fluent API或两者的组合我主要想知道如何在两个实体之间创建映射,以便正确创建外键,以及每个实体(Icollection或object)中的虚拟属性是什么样的 表名-父级 +-------------+-------+-----------------------+ | ColumnName | Type | Constraint | +-------------+----

我想使用EntityFramework6代码优先的方法创建以下两个表。我可以使用属性表示法或fluent API或两者的组合
我主要想知道如何在两个实体之间创建映射,以便正确创建外键,以及每个实体(Icollection或object)中的虚拟属性是什么样的

表名-父级

+-------------+-------+-----------------------+
| ColumnName  | Type  |      Constraint       |
+-------------+-------+-----------------------+
| ParentId    | int   | Primary Key           |
| LastChildId | int   | Foreign Key, Nullable |
+-------------+-------+-----------------------+
+----------+-------------+
| ParentId | LastChildId |
+----------+-------------+
|        1 | Null        |
|        2 | 1           |
|        3 | 3           |
+----------+-------------+
注意-LastChildId列包含最后一个ChildId,如果有子项,则对应于父项表中的父项ID

表名-子项

+------------+-------+----------------------+
| ColumnName | Type  |      Constraint      |
+------------+-------+----------------------+
| ChildId    | int   | Primary Key          |
| ParentId   | int   | ForeignKey, Not Null |
+------------+-------+----------------------+
+---------+----------+
| ChildId | ParentId |
+---------+----------+
|       1 |        2 |
|       2 |        3 |
|       3 |        3 |
+---------+----------+
范例

表-父级

+-------------+-------+-----------------------+
| ColumnName  | Type  |      Constraint       |
+-------------+-------+-----------------------+
| ParentId    | int   | Primary Key           |
| LastChildId | int   | Foreign Key, Nullable |
+-------------+-------+-----------------------+
+----------+-------------+
| ParentId | LastChildId |
+----------+-------------+
|        1 | Null        |
|        2 | 1           |
|        3 | 3           |
+----------+-------------+
表-子项

+------------+-------+----------------------+
| ColumnName | Type  |      Constraint      |
+------------+-------+----------------------+
| ChildId    | int   | Primary Key          |
| ParentId   | int   | ForeignKey, Not Null |
+------------+-------+----------------------+
+---------+----------+
| ChildId | ParentId |
+---------+----------+
|       1 |        2 |
|       2 |        3 |
|       3 |        3 |
+---------+----------+
其他信息:

  • 父级可以有多个子级与其关联,即一对多 映射(父到子)。子虚拟财产应为ICollection
  • 一个子项只能有一个父项与其关联,即一对一映射(子项到父项)
大家好,我已经在下面发布了我的答案,通过它我能够达到上述要求。
公共类儿童
{
public int ChildID{get;set;}
public int ParentID{get;set;}//这是为了正确命名密钥,
//可以省略,但EF将创建字段
//名称为smth like Parent\U ParentId
公共虚拟父级{get;set;}/**一对多
}
公共类父类
{
public int ParentId{get;set;}
公共虚拟ICollection子项{get;set;}//一对-**多**
public int LastChildID{get;set;}
}

我已经能够使用以下实体和映射文件,使用entity framework 6代码优先的方法,使用所需的映射创建所需的表

public class Parent
{
    public int ParentId {get; set;}
    public Nullable<int> LastChildId{ get; set; }
    public virtual ICollection<Child> Children{ get; set; }
    public virtual Child LastChild { get; set; }
}

public class Child
{
    public int ChildId {get;set;}
    public int ParentId { get; set; }
    public virtual Parent Parent { get; set; }
}

ParentMap.cs (Mapping File)
-----------
public class ParentMap : EntityTypeConfiguration<Entity.Parent>
{
    public ParentMap()
    {
            this.HasOptional(h => h.LastChild)
                .WithMany()
                .HasForeignKey(h => h.LastChildId)
                .WillCascadeOnDelete(false);
    }
}


ChildMap.cs (Mapping File)
-----------
public class ChildMap : EntityTypeConfiguration<Entity.Child>
{
    public ChildMap()
    {
                    this.HasRequired(t => t.Parent)
                        .WithMany(t => t.Child)
                        .HasForeignKey(t => t.ParentId)
                        .WillCascadeOnDelete(false);    
    }
}
公共类父类
{
public int ParentId{get;set;}
公共可为空的LastChildId{get;set;}
公共虚拟ICollection子项{get;set;}
公共虚拟子LastChild{get;set;}
}
公营儿童
{
public int ChildId{get;set;}
public int ParentId{get;set;}
公共虚拟父级{get;set;}
}
ParentMap.cs(映射文件)
-----------
公共类ParentMap:EntityTypeConfiguration
{
公共ParentMap()
{
this.HasOptional(h=>h.LastChild)
.有很多
.HasForeignKey(h=>h.LastChildId)
.WillCascadeOnDelete(假);
}
}
ChildMap.cs(映射文件)
-----------
公共类ChildMap:EntityTypeConfiguration
{
公共儿童地图()
{
this.HasRequired(t=>t.Parent)
.WithMany(t=>t.Child)
.HasForeignKey(t=>t.ParentId)
.WillCascadeOnDelete(假);
}
}

是否需要最后一个孩子ID?!为什么?请参阅。@HamedMoghadasi从逻辑上讲,它不是必需的。但必须以这种方式实现它,就像我们被要求这样做一样。@GertArnold你提供的链接,真的很有帮助。非常感谢你的帮助。