在 entity framework 6 代码优先方法中的两个表之间创建一对多和多对一映射

Create one to many and many to one mapping between two tables in entity framework 6 code first approach

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

Table 姓名 - Parent

+-------------+-------+-----------------------+
| ColumnName  | Type  |      Constraint       |
+-------------+-------+-----------------------+
| ParentId    | int   | Primary Key           |
| LastChildId | int   | Foreign Key, Nullable |
+-------------+-------+-----------------------+

注意-LastChildId列包含最后一个ChildId,如果有child,对应ParentId in parent table else NULL.

Table 姓名 - Child

+------------+-------+----------------------+
| ColumnName | Type  |      Constraint      |
+------------+-------+----------------------+
| ChildId    | int   | Primary Key          |
| ParentId   | int   | ForeignKey, Not Null |
+------------+-------+----------------------+

例子

Table - Parent

+----------+-------------+
| ParentId | LastChildId |
+----------+-------------+
|        1 | Null        |
|        2 | 1           |
|        3 | 3           |
+----------+-------------+

Table - Child

+---------+----------+
| ChildId | ParentId |
+---------+----------+
|       1 |        2 |
|       2 |        3 |
|       3 |        3 |
+---------+----------+

附加信息:

大家好,我把我的答案贴在下面,通过它我能够达到上面的要求。

public class Child
{
    public int ChildID { get; set; }

    public int ParentID { get; set; }  //this is to name your key properly, 
                                       //can be omitted, but EF will create field 
                                       //with name smth like Parent_ParentId

    public virtual Parent Parent { get; set; } // **one**-to-many
}

public class Parent
{
    public int ParentId { get; set; }

    public virtual ICollection<Child> Child { get; set; } //one-to-**many**

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