在启动项目中发布没有上下文的实体框架代码优先迁移 [英] 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.DataMain
- 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
我有一个 MyContext : DbContext
类位于 Main.Data
项目中.我还成功地在 Main.Data
项目上发出了 enable-migrations
console 命令,并且我成功地使用 LocalDB 作为我的数据和 upgrade-的 SQL 服务器数据库
迁移.
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.
当我尝试将 Main
项目发布到 Windows Azure 网站时,问题就开始了.
The problem starts when I am trying to publish Main
project to Windows Azure website.
使用 从 Windows Azure 网站导入
自动创建的发布配置文件似乎无法识别我使用的是实体框架代码优先解决方案,因此我无法启用 按照我的意愿执行 Code First Migrations
.
相反,我只能启用更新数据库
脚本.
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.
我使用的是带有 Entity Framework 5.0.0 的 Visual Studio 2012(自项目开始以来).
I am using Visual Studio 2012 with Entity Framework 5.0.0 (Since the beginning of the project).
只是为了验证,我尝试在 Main
项目中添加一个临时的 MyContext
类,并在 enable-migrations
上添加 >Main
项目,之后我的发布配置文件自动检测到实体框架代码优先.
那当然不是解决方案(或者是?)
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:
- Inside
Main/Web.config
我将连接字符串的名称更改为上下文类的 FQN:Main.Data.MyContext
. - 添加一个从
Main
项目到Main.Data
项目的引用(目前不需要).
- 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屋!