在启动项目中发布无上下文的实体框架代码首次迁移 [英] Publish Entity-Framework Code-First Migrations with no Context in the startup project
问题描述
我正在构建一个包含以下项目的解决方案:
I am building a solution with the following Projects:
-
Main.Data
- 类库项目 -
Main.API
- Asp.NET MVC WebApi - 引用Main.Data -
主要
- Asp.NET MVC 4 web应用程序 - 参考Main.API
Main.Data
- Class Library projectMain.API
- Asp.NET MVC WebApi - references Main.DataMain
- Asp.NET MVC 4 web application - references Main.API
我有一个位于 Main.Data
项目内的 MyContext:DbContext
类。
我也成功地在 Main.Data
项目上成功发布了 enable-migrations
console命令,我成功使用LocalDB作为我的数据的SQL服务器和 upgrade-database
迁移。
I have a MyContext : DbContext
class located inside Main.Data
project.
I have also successfully issues enable-migrations
console command on Main.Data
project, and I am successfully using LocalDB as an SQL server for my data and for upgrade-database
migrations.
问题开始于我尝试向Windows Azure网站发布主要
项目。
The problem starts when I am trying to publish Main
project to Windows Azure website.
使用<$ c $自动创建的发布配置文件c>从Windows Azure网站导入似乎不认识到我正在使用Entity Framework Code First解决方案,因此我无法启用执行代码第一次迁移
我想。
The Publish Profile that is automatically created using Import from a Windows Azure web site
does not seems to recognize that I am using Entity Framework Code First solution, and so I can't enable Execute Code First Migrations
as I would like to.
Instead, I can only enable Update database
scripts.
相反,我只能启用更新数据库
脚本。我使用Visual Studio 2012与实体框架5.0.0(自项目开始以来)。
I am using Visual Studio 2012 with Entity Framework 5.0.0 (Since the beginning of the project).
只是为了验证,我试图添加一个临时的 MyContext $ c $在
主要
项目中的c>类和主要$ <$ code>启用迁移
c $ c>项目,然后我的发布配置文件自动检测到实体框架代码优先。
这当然不是解决方案(或是吗?)
Just to verify, I have tried to add a temporary MyContext
class inside the Main
Project and to enable-migrations
on the Main
project, and after that my Publish Profile automatically detected Entity Framework Code-First.
That is, of-course, not a solution (or is it?)
以下是一些相关主题:
Here are some relevant threads:
- This is the base learning tutorial.
- This explains deployment options, but no troubleshooting.
- This actually gives a few ideas to try, but all seems unnatural.
我正在寻找一个干净稳定的解决方案。我必须将我的Context类放在主项目中?
I am looking for a clean stable solution. Do I have to put my Context class inside the Main Project?
提前感谢
推荐答案
我可以在创建发布配置文件时启用执行代码第一次迁移
。
这是我做了什么来实现的:
I can now enable Execute Code First Migrations
when I create a publish profile.
Here is what I did to achieve it:
- 内部
Main / Web.config
我将连接字符串的名称更改为上下文类的FQN:Main.Data.MyContext
。 - 从
Main
project toMain.Data
Project(直到现在还不需要)。
- Inside
Main/Web.config
I changed the name of the connection string to the FQN of the context class:Main.Data.MyContext
. - Add a reference from
Main
project toMain.Data
Project (which was not needed until now).
这是我的工作。
如果有人获得了更好或更有教育意义的答案,我很乐意听到。
This does the job for me.
If anyone got a better or more educating answer, I would be happy to hear it.
这篇关于在启动项目中发布无上下文的实体框架代码首次迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!