使用Generics使用Entity框架创建通用数据存储库 [英] Working with Generics to create a generic data repository with Entity framework

查看:65
本文介绍了使用Generics使用Entity框架创建通用数据存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一切我需要创建一个通用数据存储库,除了我无法为上下文设置泛型类型。  

I got everything down I need for create a generic data repository with the exception that I am not able to set a generic type for the context.  

我遇到了这个问题:

'TContext'不包含'Set'的定义,也没有扩展方法'设置'接受类型为'TContext'的第一个参数

尝试执行此操作时:

SetupContext().Set<TModel>()

只是一个FYI,安装上下文是这样的:

Just an FYI, Setup Context is this:

return (TContext)Activator.CreateInstance(typeof(TContext));

当我们使用每个Code First Entity Db类的Context继承DbContext时,是否可以让Generic类型识别DbContext?

As we inherit DbContext with the Context of each Code First Entity Db class, is it possible to have the Generic type recognize the DbContext?

Robert Johnston

Robert Johnston

推荐答案

嗨鲍勃约翰斯顿,

Hi Bob Johnston,

感谢您在此发帖。

根据您的问题与实体框架更相关,我将把它移到

ADO.NET Entity F ramework和LINQ to Entities
论坛提供适当的支持。

According to your question is more related to Entity Framework, I will move it to ADO.NET Entity Framework and LINQ to Entities forum for suitable support.

Visual C#讨论并询问C#编程语言,IDE,库,示例和工具。

The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

如果您有一些语法或代码错误,请随时与我们联系。我们将尽力为您提供解决方案。

If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

感谢您的理解与合作。

最诚挚的问候,

Wendy


这篇关于使用Generics使用Entity框架创建通用数据存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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