存储库模式的实体框架和子对象 [英] Repository Pattern for Entity Framework and Children Objects

查看:111
本文介绍了存储库模式的实体框架和子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下层的应用程序:

I have an application with the following layers:

具体的WebAPI控制器 - >通用的WebAPI控制器的父类 - >通用库类 - >实体框架 - >数据库

Specific WebAPI Controller -> Generic WebAPI Controller Parent Class -> Generic Repository Class -> Entity Framework -> Database

我得到的问题是,当我尝试检索与相关对象的对象,例如,博文,用类别和用户,后两者正在返回null。我在数据库中的模型(其中EF可以推断)中定义的适当关系,以及外键关系。

The problem I'm getting is that when I try to retrieve an object with related objects, for instance, BlogPost, with Category and User, the latter two are being returned null. I have the proper relationships defined at the Model (which EF can infer from) as well as foreign key relationships in the database.

我想这是因为我的通用存储库做一个只对主类获取,并且不包括其他人。

I figured this is because my generic repository does a Get only for the main class, and does not include the others.

我知道我能得到其他人,如果我把在包含,但我怎么能做到这一点与普通层?如果我重写我的方法,这将可能意味着,我不会使用通用方法大多数我的实现来。

I know I can get the others if I put in "Include", but how can I do this with generic layers? If I override my methods, that will probably mean that I won't be using the generic methods for majority of my implementation.

有另一种方式告诉EF检索子对象,而不必诉诸获取覆盖?

Is there another way to tell EF to retrieve the child objects without having to resort to Get overrides?

推荐答案

我觉得这个博文描述你所寻找什么:的传Include语句到存储库

I think this blogpost describes exactly what you are looking for: Passing Include statements into a Repository

这篇关于存储库模式的实体框架和子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆