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

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

问题描述

我的工作,在可以与多个UI版本/变种结束了一个项目,但到目前为止,我有我的解决方案,网络两个子项目 - 包含Web界面的ASP.NET MVC。服务项目是地方我有我的数据库上下文和模型定义。

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具体code引用。我想,所以当我切换的dll与后端服务(从让说SQL到XML或MySQL),它是独立的,我不应该让我的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的用户界面服务类改变。是否有位置的任何缺点?
- 我想有上下文的的connectionString要与属性/ 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 ?

推荐答案

您将需要一个机制的第一个使用前致电 Database.SetInitializer 方法的DbContext 。这就是为什么它通常被称为在的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天全站免登陆