简体   繁体   English

在 MVC5 ASP.Net 应用程序中使用 Repository 和 UnitOfWork 模式时,如何修改数据库初始值设定项 Seed 方法?

[英]How do modify database initializer Seed method when using Repository and UnitOfWork patterns in MVC5 ASP.Net application?

I've had a look through the tutorial https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application我看过教程https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing -the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

(I'm actually using MVC5 and even that's old now!! - The MVC5 version of the same tutorial didn't go into repository and unit of work much). (我实际上在使用 MVC5,即使它现在也很旧了!! - 同一教程的 MVC5 版本并没有太多涉及存储库和工作单元)。

I've implemented the patterns as described, and replaced the code in my controllers, but am left puzzling over my Database Initializer that I created earlier in the tutorial.我已经实现了所描述的模式,并替换了我的控制器中的代码,但是我对我在本教程前面创建的数据库初始化程序感到困惑。

It has a method它有一个方法

protected override void Seed( MyContext context )

It seems now that I ought to be changing the code here to use UnitOfWork too, rather than MyContext现在看来我也应该更改这里的代码以使用 UnitOfWork,而不是 MyContext

I can't do that though, because this is an overridden method and I guess the method signature has to be just as it is to be called at the right time.但是我不能这样做,因为这是一个被覆盖的方法,我猜方法签名必须就像在正确的时间调用一样。

Further, the instance of MyContext that is in UnitOfWork is private, so even if I could pass a unit of work in I still couldn't use its context.此外,UnitOfWork 中的 MyContext 实例是私有的,因此即使我可以传递一个工作单元,我仍然无法使用它的上下文。

So the question is once you've done this and implemented Repository and UnitOfWork, how do you fix up this initialization code, or how else do you initialize your database with seed data?所以问题是一旦你完成了这个并实现了 Repository 和 UnitOfWork,你如何修复这个初始化代码,或者你如何用种子数据初始化你的数据库?

So, I think this was a bit of n00b error on my part, and some of you that had looked at this probably wondered what on earth I was on about, but I'll leave this answer here just in case it helps anyone else.所以,我认为这对我来说有点 n00b 错误,你们中的一些人可能想知道我到底在说什么,但我会把这个答案留在这里以防万一它有助于其他人。

Following the tutorial I'd created a按照教程我创建了一个

public class GenericRepository<TEntity> where TEntity : class
{
    internal PayloadContext context;
    internal DbSet<TEntity> dbSet;

    public GenericRepository( PayloadContext context )
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }

    ....
}

And I hadn't really looked closely enough at that second line in the constructor.而且我还没有仔细观察构造函数中的第二行。

I'd seen that the context was now created in th UnitOfWork, and passed into the various repositories so that all the repositories could share it.我已经看到上下文现在是在 UnitOfWork 中创建的,并传递到各种存储库中,以便所有存储库都可以共享它。

But I'd thought, incorrectly, that what was happening above was the a DbSet was being created for this repository.但是我错误地认为,上面发生的事情是为这个存储库创建了一个 DbSet。 Therefore I removed the declaration of the DbSet properties from my context, thinking I'd never use them, and that I'd now be accessing the data through the repositories.因此,我从上下文中删除了 DbSet 属性的声明,认为我永远不会使用它们,并且我现在将通过存储库访问数据。

In other words in the controllers, for example, I thought I was never doing:换句话说,在控制器中,例如,我以为我从来没有这样做过:

MyContext db = new MyContext();

and

public ActionResult Index()
{
    return View( db.MySetPropertyDeclaredInMyContext.Get() )
}

but rather反而

private UnitOfWork unitOfWork = new UnitOfWork();

and

public ActionResult Index()
{
    return View( unitOfWork .MySetPropertyDeclaredInMyUoW.Get() )
}

By the time I got back to "fixing up" the initialisation code I was was stuck because that had the context passed in and the "old" entity sets directly referenced.当我回到“修复”初始化代码时,我被卡住了,因为它传入了上下文并且直接引用了“旧”实体集。 Further I don't call the Seed method myself from my own code.此外,我不会从自己的代码中自己调用 Seed 方法。

What I realised is that the second line in the constructor of the GenericRepository was only setting a reference to a DBSet (that's a method, not a 'new').我意识到 GenericRepository 的构造函数中的第二行只是设置对 DBSet 的引用(这是一种方法,而不是“新的”)。 What's more "Entity Framework requires that this method return the same instance each time that it is called for a given context instance and entity type."更重要的是“实体框架要求此方法每次为给定的上下文实例和实体类型调用时都返回相同的实例。” With that in mind, I still needed my original DbSet property declarations inside the context.考虑到这一点,我仍然需要上下文中的原始 DbSet 属性声明。

The DatabaseInitializer Seed method could remain unchanged. DatabaseInitializer Seed 方法可以保持不变。

For completeness, for those wondering where this is called: An implementation of this interface [IDatabaseInitializer] is used to initialize the underlying database when an instance of a DbContext derived class is used for the first time.为了完整起见,对于那些想知道它在哪里调用的人来说:当第一次使用 DbContext 派生类的实例时,这个接口的实现 [IDatabaseInitializer] 用于初始化底层数据库。 This initialization can conditionally create the database and/or seed it with data.这种初始化可以有条件地创建数据库和/或用数据为其播种。 The strategy used is set using the static InitializationStrategy property of the Database class使用的策略是使用 Database 类的静态 InitializationStrategy 属性设置的

So in other words, this initialisation code was fine and would be triggered first time the context was used which would be first time a UnitOfWork was performed.所以换句话说,这个初始化代码很好,并且会在第一次使用上下文时触发,这将是第一次执行 UnitOfWork。 (Or maybe even invoked). (或者甚至可能被调用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET MVC 5中使用具有通用存储库模式UnitOfWork的继承接口的方法 - Using Method of Inherited Interface with Generic Repository Pattern, UnitOfWork in ASP.NET MVC 5 在我的asp.net MVC Web应用程序中获取通用存储库+子存储库+ UnitOfWork - Getting Generic Repository + Child repository + UnitOfWork inside my asp.net mvc web application 使用async / await方法Asp.NET MVC5 EF时使用repository patern - Using repository patern when using async / await methods Asp.NET MVC5 EF 将项目插入到UnitofWork存储库-不保存对象-ASP.Net MVC - Inserting items to UnitofWork Repository - objects not saving - ASP.Net MVC 标准化存储库,UnitOfWork,IOC容器Asp.Net MVC - Standardize Repository, UnitOfWork, IOC Container Asp.Net MVC 更改现有Asp.Net MVC 5(身份2)应用程序的种子方法? - Change seed method for an existing Asp.Net MVC 5 (Identity 2) application? 在更新数据库期间,ASP.NET MVC初始化程序不会为数据库设置种子 - ASP.NET MVC Initializer doesn't seed Database during Update-Database ASP.NET MVC5除了在初始化程序处中断外,不会创建MySQL数据库 - ASP.NET MVC5 doesn't create MySQL database except break at the initializer ASP.NET MVC5数据库 - ASP.NET MVC5 database 如何在ASP.NET MVC5应用程序中处理请求超时? - How to handle a request timeout in ASP.NET MVC5 application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM