放置 Database.SetInitializer 的地方 [英] Place to put Database.SetInitializer

查看:23
本文介绍了放置 Database.SetInitializer 的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,该项目最终可能有多个 UI 版本/变体,但到目前为止,我的解决方案 Web 中有两个子项目 - 包含带有 ASP.NET MVC 的 Web 界面.服务项目是我定义数据库上下文和模型的地方.

I'm working on a project that at can end up with multiple UI versions / variants, but so far I've got two subprojects in my solution Web - containing Web interface with ASP.NET MVC. Service project is place where I have my database context and models defined.

我的目标是在我的 Web 项目中最少或可能没有对 EF 特定代码的引用.我希望它是独立的,所以当我用服务后端(从 SQL 到 XML 或 MySQL)切换 dll 时,我不应该在我的 MVC 项目中进行多次修改.

My Goal is to have minimum or possibly no references to EF specific code in my Web project. I want it to be independent so when I switch the dlls with service backend ( from let say SQL to XML or MySQL ) I shouldn't make multiple modifications in my MVC project.

这是它的样子:

我的问题是:- 到目前为止,我没有发现在 Global.asax 之外的其他地方使用 Database.SetInitializer 的例子.如果模型在我的类似工厂的 DatabaseContextProvider 类或从上下文中提取数据并将其提供给具有 DTO 的 UI 的服务类中发生更改,我想重新创建数据库.那个位置有什么缺点吗?- 我想让上下文的 connectionString 可以通过 Properties/Settings.settings 文件进行配置 - 这合理吗?

My Questions are: - so far I've found no example of using Database.SetInitializer in other place than Global.asax. I'd like to put database re-creation if model changed in my factory-like DatabaseContextProvider class or in service class that pulls out data from context and provides it to the UI with DTOs. Are there any cons of that location ? - I would like to have the context's connectionString to be configurable with Properties/Settings.settings file - is that reasonable ?

推荐答案

在第一次使用 DbContextDatabase.SetInitializer 方法>.这就是它通常在 Global.asax 文件中调用的原因.

You would need a mechanism to call the Database.SetInitializer method before the very first usage of the DbContext. That is why its usually called in the Global.asax file.

您可以在您的 tm.Service 项目中创建一个带有初始化方法的类,并在 Application_Start 方法中调用它并放置 Database.SetInitializer 在那个初始化方法中.

You can create a class with an initialization method in your tm.Service project and call it in the Application_Start method and put the Database.SetInitializer in that initialization method.

可以从设置文件中提供连接字符串.

Its OK to supply the connection string from a setting file.

这篇关于放置 Database.SetInitializer 的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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