EntityFramework快速添加嵌套实体

时间:2017-02-03 11:27:11

标签: entity-framework

我在数据库中有这些模型。我有一系列代表这些模型和关系的POCO。我需要将它们添加到数据库并创建它们之间的关系。所有对象数都是200k。最有效的方法是什么?

public class A
{
    private ICollection<B> children;

    public A()
    {
        this.children = new HashSet<B>();
    }

    public int Id { get; set; }

    public virtual ICollection<B> Children
    {
        get { return this.children; }
        set { this.children = value; }
    }
}

public class B
{
    private ICollection<C> children;

    public B()
    {
        this.children = new HashSet<C>();
    }

    public int Id { get; set; }

    public int AId { get; set; }

    public virtual A A { get; set; }

    public virtual ICollection<C> Children
    {
        get { return this.children; }
        set { this.children = value; }
    }
}

public class C
{
    private ICollection<D> children;

    public C()
    {
        this.children = new HashSet<D>();
    }

    public int Id { get; set; }

    public int BId { get; set; }

    public virtual B B { get; set; }

    public virtual ICollection<D> Children
    {
        get { return this.children; }
        set { this.children = value; }
    }
}

public class D
{
    private ICollection<E> children;

    public D()
    {
        this.children = new HashSet<E>();
    }

    public int Id { get; set; }

    public int CId { get; set; }

    public virtual C C { get; set; }

    public virtual ICollection<E> Children
    {
        get { return this.children; }
        set { this.children = value; }
    }
}

public class E
{
    public int Id { get; set; }

    public int DId { get; set; }

    public virtual D D { get; set; }
}

1 个答案:

答案 0 :(得分:0)

如果您的意思是最快的方式,请使用原始ADO.NET,甚至考虑使用SqlBulkCopy。

您还可以考虑将TVP与原始ADO.NET一起使用,而不是使用BULK COPY