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

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

问题描述

我开始新的ASP.NET项目,我试图按照href=\"http://stackoverflow.com/a/7474357/1080891\">多项目的方法,我已经看到了#1多个问题中提到。

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.

我一直在下面<一个href=\"http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/create_the_data_access_layer\"相对=nofollow>本教程,但它假定只有一个为整个解决方案项目。最值得注意的是,报告建议修改与code您的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一个引导程序的类,它的数据库的东西给你。

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

中的示例从下面的博客帖子拉:<一href=\"http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-$c$c-smell.aspx\" rel=\"nofollow\">http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-$c$c-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中要做的事情,以及你将在博客中看到的。此外,<一个href=\"http://stackoverflow.com/questions/3843802/how-should-i-implement-an-mvc-bootstrapper-for-unity-and-automapper\">this问题可以借上布置的细节更多的信息,等方面有更多的好处不仅仅是不必为什么使用这种模式引用DAL与周围的社区一些优秀的职位,以boostrapping是好事,以及作为执行几种不同的方法。

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天全站免登陆