Help us understand the problem. What is going on with this article?

EntityFrameworkのInclude拡張ライブラリ

More than 3 years have passed since last update.

EntityFrameworkで関連エンティティをロードするInclude()を拡張するライブラリを作りました。
EntityFramework.Include

NuGetからインストールできます。
https://www.nuget.org/packages/EntityFramework.Include

PM> Install-Package EntityFramework.Include

READMEを見れば使い方はわかると思いますが、Includeで関連エンティティのコレクションを読み込むとき、フィルターで条件を指定して読み込む機能を実装しています。

内部で何をしているか

以下のようなエンティティがあったとき

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

    public int Age { get; set; }

    [NotMapped]
    public int ChildrenCount { get; set; }

    public List<Child> Children { get; set; } = new List<Child>();
}

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

    public int Age { get; set; }
}

このような操作では

context.ParentSet.Include(p => p.Children, p => p.Children.Take(10).ToList())
                 .Include(p => P.ChildrenCount, p => p.Children.Count)
                 .ToListWithInclude();

TypeBuilderParentを継承したclassを作り
式木をこねて

Select(p => new Parent_継承()
                {
                    Id = p.Id,
                    Age = p.Age,
                    Children = p.Children.Take(10).ToList(),
                    ChildrenCount = p.Children.Count
                })

相当のExpressionを組み立ててToList()
最後にParentにキャストしてListを返しています。
返ってきたエンティティたちはDetached状態なので注意が必要です。

バグレポートだったりほしい機能を作者Twitterに投げてくれるとうれしいのでよろしくお願いします。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした