在另一个项目中使用上下文发布实体框架代码优先迁移

Publish Entity-Framework Code-First Migrations with Context in another project

我描述了同样的问题 here 并且我已经尝试应用建议的解决方案,将连接字符串名称更改为我的数据库上下文的 FQN,它被放置在另一个项目中。

Web 项目 Web.config 文件(在项目 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(在项目 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 年不再有效:​​(

有人可以帮我吗?

提前谢谢大家!

EF 代码优先工具 (> EF 6.X) 运行良好,支持开箱即用的用例,允许开发人员通过分离 DB/EF 来遵循最佳实践来自网络 context/projects。这实际上是了解工具并以正确的方式做事的问题。我建议您设置一个新的解决方案来尝试代码优先迁移。如果您没有以孤立的方式进行此操作,请不要浪费时间在您的真实 project/solution.

中一遍又一遍地进行测试。

在我们的项目中设置代码优先迁移时,我的团队得到了 Microsoft 员工的支持。他们做对了,我们在文档中保存了一些内容。

在浏览 Internet 时,我发现这些页面很有用,我知道在处理 EF 代码优先开发和部署(尤其是使用 Visual studio 部署到 Azure 应用程序服务)方面有很多帮助。

由于您的问题很难回答,我建议您遵循为 EF 6.X 和 VS2015 编写的最佳实践和文档,并在更好地了解解决问题所需的工具后返回 SO问题。然后要么编辑你的问题,要么问另一个问题。

为了给您一些开胃菜,只需查看 migrate.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

根据您传递给迁移工具的配置参数,您可以运行 显式、基于配置文件或基于约定的迁移。密切注意 migrate 打印的内容作为 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**).

我找到了解决问题的方法。我创建了一个新的新项目,一个一个地添加文件和依赖项,我发现哪些使选项消失了。

他们是:

  • Autofac.Owin
  • Autofac.Mvc.Owin
  • Autofac.WebApi2.Owin

和一个从包 PerpetuumSoft.Knockout.

继承自 KnockoutController 的控制器

我删除了这些依赖项,现在发布对话框中的代码优先选项再次可见并可激活。

不知道为什么这些依赖项会导致该问题,我没有看到任何相关性,但现在它正在按预期工作。 :)