在另一个项目中发布带有上下文的实体-框架代码优先迁移
本文关键字:框架 实体 代码 迁移 上下文 项目 另一个 布带 | 更新日期: 2023-09-27 17:53:23
我在这里描述了同样的问题,我已经尝试应用建议的解决方案,将连接字符串名称更改为放置在另一个项目中的db上下文的FQN。
Web项目Web。配置文件(在MyApplication.Web项目中):
<connectionStrings>
<add name="FarmaciaNataliniServer.Infrastructure.ApplicationDbContext" connectionString="Data Source=(LocalDb)'MSSQLLocalDB;AttachDbFilename=|DataDirectory|'aspnet-FarmaciaNataliniServer-20151127115838.mdf;Initial Catalog=aspnet-FarmaciaNataliniServer-20151127115838;Integrated Security=True" providerName="System.Data.SqlClient" />
ApplicationDbContext.cs (project MyApplication):
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<RegisteredDevice> RegisteredDevices { get; set; }
public DbSet<Reservation> Reservations { get; set; }
public ApplicationDbContext() : base("FarmaciaNataliniServer.Infrastructure.ApplicationDbContext", throwIfV1Schema: false)
{
}
...
不幸的是,这个解决方案似乎不再在Visual Studio 2015上工作了:(
有谁能帮我一下吗?提前感谢大家!
我找到了解决问题的方法。我创建了一个新的新项目,一个接一个地添加文件和依赖项,我发现哪些使选项消失。
他们:- Autofac。Owin
- Autofac.Mvc.Owin
- Autofac.WebApi2.Owin
和从KnockoutController继承而来的Controller。
我已经删除了这些依赖,现在发布对话框上的Code-First选项是可见的,并且可以再次激活。
不知道为什么这些依赖关系会导致这个问题,我没有看到任何相关性,但现在它按预期工作。:)
EF代码首先工具(> EF 6)。X)工作得非常好,并且支持开箱即用的用例,允许开发人员通过将DB/EF与Web上下文/项目分离来遵循最佳实践。这实际上是一个理解工具和以正确的方式做事的问题。我建议您设置一个新的解决方案,只尝试代码优先迁移。如果你不能以一种孤立的方式让它工作,不要浪费你的时间在你的实际项目/解决方案中一遍又一遍地测试。
在我们的项目中设置代码优先迁移时,我的团队得到了微软员工的支持。他们只是做得对,我们在文档中保存了一些比特。
在浏览互联网时,我发现这些页面很有帮助,我知道有很多帮助来处理EF代码优先的开发和部署(特别是使用Visual studio部署到Azure App服务)。
由于您的问题很难回答,我建议您遵循为EF 6编写的最佳实践和文档。X和VS2015,然后带着对解决问题所需的工具的更好理解回到SO。然后编辑你的问题或者直接问别人。
- 在ASP中使用实体框架进行代码优先迁移和部署。. NET MVC应用
- 代码优先迁移命令(编写于2014年-但很好地介绍了运行代码优先迁移所需的命令以及如何在包管理器控制台获得EF迁移命令的帮助)
要给您一些开胃菜,只需看看migration .exe的帮助—一个从命令行运行代码优先迁移的工具。这是你可能需要的一切。
migrate.exe
Code First Migrations Command Line Utility
Applies any pending migrations to the database.
migrate assembly [configurationType] [contextAssembly] [/targetMigration]
[/startUpDirectory] [/startUpConfigurationFile]
[/startUpDataDirectory] [/connectionStringName]
[/connectionString] [/connectionProviderName] [/force] [/verbose]
[/?]
assembly Specifies the name of the assembly that
contains the migrations configuration type.
[configurationType] Specifies the name of the migrations
configuration type. If omitted, Code First
Migrations will attempt to locate a single
migrations configuration type in the specified
assembly.
[contextAssembly] Specifies the name of the assembly that
contains the DbContext type if different from
the assembly that contains the migrations
configuration type.
[/?] Display this help message.
[/connectionProviderName] Specifies the provider invariant name of the
connection string.
[/connectionString] Specifies the connection string to use. If
omitted, the context's default connection will
be used.
[/connectionStringName] Specifies the name of the connection string to
use from the specified configuration file. If
omitted, the context's default connection will
be used.
[/force] Indicates that automatic migrations which might
incur data loss should be allowed.
[/startUpConfigurationFile] Specifies the Web.config or App.config file of
your application.
[/startUpDataDirectory] Specifies the directory to use when resolving
connection strings containing the
|DataDirectory| substitution string.
[/startUpDirectory] Specifies the working directory of your
application.
[/targetMigration] Specifies the name of a particular migration to
update the database to. If omitted, the current
model will be used.
[/verbose] Indicates that the executing SQL and additional
diagnostic information should be output to the console window
根据您传递给迁移工具的配置参数,您可以运行显式的、基于配置文件的或基于约定的迁移。密切关注作为Origin
1。显式参数
migrate.exe "Fireframework.Web.dll" "Fireframework.Web.Migrations.Configuration" /connectionString="Data Source=tcp:fireframeworkdbs.database.windows.net,1433;Initial Catalog=fireframeworkdbdev;User Id=ffdbadmin@fireframeworkdbs;Password="secret" /connectionProviderName="System.Data.SqlClient" /verbose
2016-04-25T08:06:09.9183260Z VERBOSE: Target database is: 'fireframeworkdbdev' (DataSource: tcp:fireframeworkdbs.database.windows.net,1433, Provider: System.Data.SqlClient, **Origin: Explicit**).
2016-04-25T08:06:13.5640428Z No pending explicit migrations.
2。配置文件
migrate.exe "Fireframework.Web.dll" "Fireframework.Web.Migrations.Configuration" /startUpConfigurationFile="web.config" /verbose
VERBOSE: Target database is: 'fireframework' (DataSource: (LocalDb)'MSSQLLocalDB, Provider: System.Data.SqlClient, **Origin: Configuration**).
Applying explicit migrations: [201603161334138_InitialCreate, 201604081002396_Add_Simulation_CreatedDateTime_Property, 201604201138442_Add_Simulation_AbortRequestPending_Property, 201604211315107_Rename_Simulation_Properties].
Applying explicit migration: 201603161334138_InitialCreate.
3。按照惯例
migrate.exe "Fireframework.Web.dll" "Fireframework.Web.Migrations.Configuration"
VERBOSE: Target database is: 'Fireframework.Model.DatabaseContext' (DataSource: .'SQLEXPRESS, Provider: System.Data.SqlClient, **Origin: Convention**).