在独立的DAL项目中播种EF数据库 [英] Seeding EF Database in a Separate DAL Project

查看:160
本文介绍了在独立的DAL项目中播种EF数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始一个新的ASP.NET项目,我试图遵循我所看到的多项目方法在Stackoverflow的多个问题中提到了这个问题。

I'm starting a new ASP.NET project, and I'm trying to follow the multi-project approach I've seen mentioned in multiple questions around Stackoverflow.

我一直在追踪这个教程,但它只假定整个解决方案只有一个项目。最值得注意的是,它建议使用代码设置数据库初始化程序来修改Global.asax文件。

I've been following this tutorial, but it assumes only one project for your entire solution. Most notably, it recommends modifying your Global.asax file with code to set the database initializer.

看到我的DAL项目没有Global.asax文件,原始EF数据库的正确程序是什么?

Seeing as how my DAL project doesn't have a Global.asax file, what is the correct procedure for seeding the initial EF database?

推荐答案

在上述评论中我已经纠正了,您可以访问DAL通过二次参考。如果您真的希望不要在您的Web项目中引用DAL,请在您的BLL中创建一个Bootstrapper类,为您提供数据库的东西

I stand corrected in my above comment that you would have access to the DAL by secondary reference. If you are truly wanting to keep from referencing the DAL in your web project, create a Bootstrapper class in your BLL that does the database stuff for you

示例从以下博客文章: http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and -reduce-code-smell.aspx

The examples are pulled from the following blog post: http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-code-smell.aspx

创建引导界面

public interface IBootstrapperTask
{
    void Execute();
}

创建一个处理数据库配置的类

Create a class that would handle your database configuration

public class InitializeDatabase : IBootstrapperTask
{
    public void Execute()
    {
        Database.SetInitializer(new Configuration());
        using (var db = new Context())
        {
          try
          {
            db.Database.Initialize(false);
          }
          catch (DataException ex)
          {

          }
          catch (Exception ex)
          {
            throw;
          }
        }
      }
       }

创建一个静态类将执行任务(您可以有多个注册路由可以移动到一个BootstrapperTask)

Create a static class that will execute the tasks (you can have more than one, registering routes could be moved to a BootstrapperTask)

public static class Bootstrapper
{
    static Bootstrapper()
    {
        ConfigureContainer();
    }

    public static void Run()
    {
        var tasks = ServiceLocator.Current.GetAllInstances<IBootstrapperTask>();

        foreach(var task in tasks)
        {
            task.Execute();
        }
    }

    private static void ConfigureContainer()
    {
        IUnityContainer container = new UnityContainer();

        UnityConfigurationSection configuration = (UnityConfigurationSection) ConfigurationManager.GetSection("unity");
        configuration.Containers.Default.Configure(container);

        ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
    }
}

最后,你的global.asax会有一个班轮

Finally, your global.asax would have a one liner

protected void Application_Start()
{
    Bootstrapper.Run();
}

有一些web.config要做的事情,你会看到博客帖子。此外,此问题可以借出一些关于处理细节的信息等。对于欺骗来说,更多的好处不仅仅是简单地不用参考一个DAL,而是在社区中提供一些优秀的帖子,为什么使用这种模式是一件好事,还有一些不同的实现方法。

There are some web.config things to do as well that you will see in the blog posting. Also, this question can lend some more information on the specifics of disposing, etc. There are more benefits to boostrapping than simply not having to reference a DAL with some excellent posts around the community on why using this pattern is a good thing as well as a few different approaches on implementation.

这篇关于在独立的DAL项目中播种EF数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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