Repository模式与实体框架和MVC4建立动态连接字符串 [英] Repository Pattern with Entity Framework and MVC4 Building Dynamic Connection String

查看:450
本文介绍了Repository模式与实体框架和MVC4建立动态连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临的一个问题,而与EF6实施MVC4 Repository模式[UOW]

I am facing an issue while implementing the Repository Pattern [UoW] for MVC4 with EF6.

错误:XX.DataAccess.Context必须是一个非抽象类型与公共参数构造函数,以便用它作为泛型类型或方法参数TContext'XX.DataAccess.WriteRepository

Error: 'XX.DataAccess.Context' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TContext' in the generic type or method 'XX.DataAccess.WriteRepository'

//Data Access layer Class for Saving/Deleting/Retriving etc. Inherits WriteRepository
public class Common : WriteRepository<Context>, ICommon
{
    //Method
}

//Repository Pattern
public abstract class WriteRepository<TContext> : IWriteRepository where TContext : DbContext, new()
{
    private readonly TContext _context;

    protected TContext Context { get { return _context; } }

    protected WriteRepository()
    {
        _context = new TContext();
    }

//Save Method

//Delete Method

//Retrive Method

//Find Method
}

// DB上下文类
   //这里我需要建立动态的连接字符串,这需要作为参数的客户端ID。如果我在参数中使用它,它给了我这是上面提到的数据访问方法实现的错误。

//DB Context Class //Here i need to build dynamic connection string which takes the Client ID as the Parameter. If i use it in the parameter it gives me the error for Data Access method implementations which is mentioned above.

public partial class Context : DbContext
{
    static Context ()
    {
        Database.SetInitializer<Context>(null);
    }

    public Context (int ClientID = 0)
        : base(ConnectionString(ClientID))
    {
        var objContext = (this as IObjectContextAdapter).ObjectContext;
        objContext.CommandTimeout = 180;
    }

//DBSet's

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    //Mapping
}

private static string ConnectionString(int ClientID = 0)
    {
    //return connection string
}
}

让我知道做什么样的变化,我需要,使其工作。

Let me know what changes do i need to make it work.

推荐答案

的问题是新()约束 WriteRepository 。约束不能满足,因为上下文不具有零参数的构造函数,因为你需要通过客户端ID 中当您创建它。因此,删除新()约束,而是修改 WriteRepository 构造带类型的参数 TContext

The problem is the new() constraint on WriteRepository. The constraint cannot be satisfied because Context does not have an zero-argument constructor, since you need to pass the ClientID in when you create it. Therefore, remove the new() constraint, and instead modify the WriteRepository constructor to take an argument of type TContext:

//Repository Pattern
public abstract class WriteRepository<TContext> : IWriteRepository where TContext : DbContext
{
    private readonly TContext _context;

    protected TContext Context { get { return _context; } }

    protected WriteRepository(TContext context)
    {
        _context = context;
    }

    //Save Method

    //Delete Method

    //Retrive Method

    //Find Method

}

然后,在派生类中,创建上下文并把它作为参数传递给基构造。

Then, in the derived class, create the Context and pass it in as an argument to the base constructor.

//Data Access layer Class for Saving/Deleting/Retriving etc. Inherits WriteRepository
public class Common : WriteRepository<Context>, ICommon
{
    public Common(int clientID)
        :base(new Context(clientID))
    {

    }
}

当然,这已经从 WriteRepository 派生的任何其他类将需要以及修改。我希望这有助于!

Of course, any other classes that already derive from WriteRepository will need to be modified as well. I hope this helps!

这篇关于Repository模式与实体框架和MVC4建立动态连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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