实体框架5图书馆消费者需要实体框架dll? [英] Entity Framework 5 library consumers need entity framework dlls?

查看:109
本文介绍了实体框架5图书馆消费者需要实体框架dll?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目Project.EntityFramework,其中包含一个上下文到我的数据库。在同一解决方案Project.Business中尝试从单独的项目中消耗上下文,如:

  using(var db =新EntityFramework.Entities())
{
//代码
}



 错误12'EntityFramework.Entities':在使用中使用的类型语句必须隐式转换为System.IDisposable.... 

现在我可以F12和钻看到它正在实现IDisposable - 如果我将参考文件dll添加到Project.Business中的实体框架中,我可以将此错误消失,但是这样会打破我想要完成的目标 - 没有任何依赖在我的业务层内的EF。



我做错了什么?或者这是怎么回事?

解决方案

要扩大@Spivonious的答案,这里是我做了什么我的DbContext上的存储库模式 - 希望它主要实现:/

  public interface IFooEntity:IDisposable 
{
IEnumerable< Foo>得到();
}

public class FooRepository:IFooEntity
{
private readonly Entities _context;

public FooService()
{
this._context = new Entities();
}

public IEnumerable&Foo> Get()
{
return _context.Foo;
}

public void Dispose()
{
_context.Dispose();
}
}

原始代码来自问题:

  using(var db = new EntityFramework.Entities())
{
//代码
}

更改为:

  using(var db = new EntityFramework.Repository.FooRepository())
{
//代码
}

这允许我的代码在单独的项目中使用我的存储库而不是上下文本身,允许业务项目不必依靠实体框架引用。


I have a Project "Project.EntityFramework" which contains a context to my db. When attempting to consume the context from a separate project within the same solution "Project.Business" like:

using (var db = new EntityFramework.Entities())
{
    // Code
}

I get the compiler error on "using":

Error   12  'EntityFramework.Entities': type used in a using statement must be implicitly convertible to 'System.IDisposable'   ....

Now I can F12 and drill down to the Entities, and see it is implementing IDisposable - I can make this error go away if I put reference dlls to entity framework in "Project.Business" but this is defeating the purpose of what I wanted to accomplish - not having any dependency on EF within my Business layer.

Am I doing something wrong? Or is this just how it has to be?

解决方案

To expand on @Spivonious' answer, here is what I did to accomplish the repository pattern on my DbContext - hopefully it's mostly implemented properly :/

public interface IFooEntity : IDisposable
{
    IEnumerable<Foo> Get();
}

public class FooRepository : IFooEntity
{
    private readonly Entities _context;

    public FooService()
    {
        this._context = new Entities();
    }

    public IEnumerable<Foo> Get()
    {
        return _context.Foo;
    }

    public void Dispose()
    {
        _context.Dispose();
    }
}

original code from question:

using (var db = new EntityFramework.Entities())
{
    // Code
}

changed to:

using (var db = new EntityFramework.Repository.FooRepository())
{
    // Code
}

this allowed my code in the separate project to use my repository rather than the context itself, allowing the business project to not have to rely on entity framework references.

这篇关于实体框架5图书馆消费者需要实体框架dll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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