使用实体框架和 MVC4 构建动态连接字符串的存储库模式 [英] Repository Pattern with Entity Framework and MVC4 Building Dynamic Connection String

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

问题描述

我在使用 EF6 为 MVC4 实现存储库模式 [UoW] 时遇到问题.

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

错误:'XX.DataAccess.Context' 必须是具有公共无参数构造函数的非抽象类型,才能将其用作泛型类型或方法 'XX.DataAccess.WriteRepository' 中的参数 'TContext'

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
}

//数据库上下文类//这里我需要构建以客户端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 上的 new() 约束.约束不能被满足,因为 Context 没有零参数构造函数,因为你需要在创建它时传入 ClientID .因此,删除 new() 约束,改为修改 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

}

然后,在派生类中,创建 Context 并将其作为参数传递给基本构造函数.

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!

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

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