简体   繁体   English

EF 5迁移无法连接到我们的数据库,即使它在运行时运行良好

[英]EF 5 Migrations cannot connect to our database even though it does just fine at runtime

We have three projects. 我们有三个项目。

  • Company.Domain (class library) Company.Domain(类库)
  • Company.PublicWebsite (MVC3 Web Application) Company.PublicWebsite(MVC3 Web Application)
  • Company.InternalWebsite (MVC3 Web Application) Company.InternalWebsite(MVC3 Web应用程序)

The two website projects have reference to Company.Domain . 这两个网站项目都引用了Company.Domain

Our EF 5 DbContext lives in Company.Domain.Data.EntityFramework and it looks like this: 我们的EF 5 DbContext位于Company.Domain.Data.EntityFramework ,它看起来像这样:

using System.Data.Entity;
using Company.Domain.Data.EntityFramework.Entities;

namespace Company.Domain.Data.EntityFramework.
{
    public class CompanyEntities : DbContext
    {
        public DbSet<Notification> Notifications { get; set; }
        public DbSet<Report> Reports { get; set; }
        public DbSet<ReportSection> ReportSections { get; set; }
        public DbSet<ReportPage> ReportPages { get; set; }
        // brevity brevity
    }
}

We have enabled migrations and successfully used the tool in the past so I'm not sure why we are having issues now. 我们已经启用了迁移并且过去成功使用了该工具,因此我不确定为什么我们现在遇到问题。 Our migrations configuration lives in Company.Domain.Data.EntityFramework.Migrations and looks like this: 我们的迁移配置位于Company.Domain.Data.EntityFramework.Migrations ,如下所示:

namespace Company.Domain.Data.EntityFramework.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using Company.Domain.Data.EntityFramework;

    public class Configuration : DbMigrationsConfiguration<CompanyEntities>
    {
        public Configuration()
        {
            MigrationsDirectory = @"Data\EntityFramework\Migrations";
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(CompanyEntities context)
        {
            // empty at the moment
        }
    }
}

We then have an App.config file in the root of the Company.Domain project and it looks like this: 然后,我们在Company.Domain项目的根目录中有一个App.config文件,它看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <configSections>
      <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    </configSections>
    <connectionStrings>
      <add name="CompanyEntities" providerName="System.Data.SqlClient" connectionString="Data Source=devsql;Initial Catalog=CompanyEntities;uid=XXXXX;pwd=XXXXX;MultipleActiveResultSets=True;" />
    </connectionStrings>
    <entityFramework>
      <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameters>
          <parameter value="v11.0" />
        </parameters>
      </defaultConnectionFactory>
    </entityFramework>
    <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  </configuration>

Our database lives on another server on the network. 我们的数据库位于网络上的另一台服务器上。 I'm able to connect to it in SQL Server Management Studio and our applications are able to connect at runtime just fine. 我能够在SQL Server Management Studio中连接它,我们的应用程序能够在运行时连接就好了。 However, when I try to run add-migration or even update-database I get the following error: 但是,当我尝试运行add-migration甚至update-database我收到以下错误:

http://content.screencast.com/users/Chevex/folders/Snagit/media/80fbfd6a-4956-407f-b88f-d5a53a9e5feb/03.21.2013-10.25.png http://content.screencast.com/users/Chevex/folders/Snagit/media/80fbfd6a-4956-407f-b88f-d5a53a9e5feb/03.21.2013-10.25.png

System.Data.ProviderIncompatibleException: An error occurred while getting provider information from the database. System.Data.ProviderIncompatibleException:从数据库获取提供程序信息时发生错误。 This can be caused by Entity Framework using an incorrect connection string. 这可能是由实体框架使用不正确的连接字符串引起的。 Check the inner exceptions for details and ensure that the connection string is correct. 检查内部异常以获取详细信息,并确保连接字符串正确。 ---> System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.Data.ProviderIncompatibleException:提供程序未返回ProviderManifestToken字符串。 ---> System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. ---> System.Data.SqlClient.SqlException:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。

I've even reverted my changes to the model and then ran update-database just to see if it would say 'database is on latest migration' but I still got the above error. 我甚至将我的更改恢复到模型然后运行update-database只是为了看看它是否会说“数据库是最新迁移”但我仍然遇到上述错误。 I've poured over the connection string in App.config over and over. 我一遍又一遍地倾倒在App.config的连接字符串。 I cannot figure out why migrations won't connect but both of our website projects work just fine at runtime. 我无法弄清楚为什么迁移不会连接但我们的两个网站项目在运行时都能正常工作。 Migrations have worked in the past. 迁移过去一直有效。 Below are the migrations in solution explorer compared with those found in the __MigrationHistory table in the database. 以下是解决方案资源管理器中的迁移与数据库中__MigrationHistory表中的迁移相比较。

http://content.screencast.com/users/Chevex/folders/Snagit/media/7abeaa46-ff0f-4817-a0d7-1adb086e8f0c/03.21.2013-10.30.png http://content.screencast.com/users/Chevex/folders/Snagit/media/7abeaa46-ff0f-4817-a0d7-1adb086e8f0c/03.21.2013-10.30.png

http://content.screencast.com/users/Chevex/folders/Snagit/media/3c6ac54d-f63d-417f-9253-39b6a8fea85d/03.21.2013-10.32.png http://content.screencast.com/users/Chevex/folders/Snagit/media/3c6ac54d-f63d-417f-9253-39b6a8fea85d/03.21.2013-10.32.png

It looks like I should be able to run update-database and have it tell me that it is up to date, but I get that error instead. 看起来我应该能够运行update-database并让它告诉我它是最新的,但我得到了那个错误。

As I understand it, migrations shouldn't be paying any attention to our two website projects when I'm running migrations commands, but I poured over their Web.config files as well just in case. 根据我的理解,当我运行迁移命令时,迁移不应该关注我们的两个网站项目,但是为了以防万一我也倾倒了他们的Web.config文件。 The connection strings are identical to App.config. 连接字符串与App.config相同。

Edit: 编辑:

I've even tried completely uninstalling and removing the EF 5 package from the projects and reinstalling. 我甚至尝试从项目中完全卸载并删除EF 5软件包并重新安装。 Same issue >:( 同样的问题> :(

您的启动项目是否包含web.config或app.config作为EF使用start项目作为连接字符串的源

OK, so that didn't work for me at first :( 好的,所以起初对我不起作用:(

Then after a cup of coffee and adding StartUPProjectName to it, it did! 然后在喝了一杯咖啡并将StartUPProjectName添加到它之后,它就完成了! Try: 尝试:

Update-Database -StartUpProjectName MYPROJECT.NAME -Script Update-Database -StartUpProjectName MYPROJECT.NAME -Script

Try to point it to a start Up project where you web.config/app.config lives 尝试将其指向web.config / app.config所在的启动项目

If you get the help for enable migrations in the Package Manager Console 如果您在Package Manager控制台中获得了启用迁移的帮助

Get-Help enable-migrations -detailed

You can find this documentation for the -StartupProjectName option: 您可以在-StartupProjectName选项中找到此文档:

-StartUpProjectName -StartUpProjectName

  Specifies the configuration file to use for named connection strings. If omitted, the specified project's configuration file is used. 

The doc it's a little confusing, but it means that if you use this option to specify a project name, migrations will look for the connection string in the config file of the specified project. 该文档有点令人困惑,但这意味着如果使用此选项指定项目名称,则迁移将在指定项目的配置文件中查找连接字符串。 (The config file can be web.config or app.config ). (配置文件可以是web.configapp.config )。

If you're creating a web app, most probably the connection string will be in its web.config , so you have to specify the web app project. 如果您正在创建Web应用程序,则很可能连接字符串将位于其web.config ,因此您必须指定Web应用程序项目。 If it's other kind of project, the connection string will be in an app.config file of a class library or whatever, and you'll have to specify that project. 如果是其他类型的项目,连接字符串将位于类库的app.config文件或其他任何内容中,您必须指定该项目。

Besides it's recommended that you use a constructor for your DbContext class that specifies the name of the connection string, ie 此外,建议您使用DbContext类的构造函数来指定连接字符串的名称,即

public class CompanyEntities : DbContext
{
    public CompanyEntities()
       :base("ConnectionStringName")
    {
         ...
    }
....
}

In this way you don't depend on default connection strings, which may be confusing. 通过这种方式,您不依赖于默认连接字符串,这可能会令人困惑。

You say you can connect via SQL Management Studio, but my guess is you use Windows Authentication for that, and not the uid=XXXXX;pwd=XXXXX; 你说你可以通过SQL Management Studio连接,但我猜你是使用Windows身份验证,而不是uid=XXXXX;pwd=XXXXX; supplied in your connection string. 在您的连接字符串中提供。

Try to get Management Studio to connect using that userid and password. 尝试让Management Studio使用该用户标识和密码进行连接。

Also, that account might be locked out (if it is an Active Directory account). 此外,该帐户可能被锁定(如果它是Active Directory帐户)。

This sounds eerily like a problem a client of mine had. 这听起来像我的客户的问题。 It had to do with something having mucked up the DbProviders section of machine.config. 它与搞砸了machine.config的DbProviders部分的东西有关。 He put the details here: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/f2a19487-6d38-4a79-a809-d3efe4c9d9fe (it's the Adam Scholz answer to his boss' question. :) ) 他把详细信息放在这里: http//social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/f2a19487-6d38-4a79-a809-d3efe4c9d9fe (这是Adam Scholz回答他老板的问题。: ))

Julie 朱丽叶

May be this is solved already but i got it solved by setting the start up project in my solution to the entity dll project ( having app.config file ). 可能这已经解决了但是我通过在我的解决方案中将启动项目设置为实体dll项目(具有app.config文件)来解决它。 I did set the "Default project" in Package Manager Console window to the correct entity dll project but that did't work. 我确实在Package Manager Console窗口中将“Default project”设置为正确的实体dll项目,但这不起作用。 For details 详情

Entity Framework 6 with SQL Server 2012 gives System.Data.Entity.Core.ProviderIncompatibleException system-data-entity-core-providerin 带有SQL Server 2012的实体框架6提供System.Data.Entity.Core.ProviderIncompatibleException system-data-entity-core-providerin

If your solution has multiple projects, try setting the Startup Project for the solution to the project that contains the Web.Config or App.Config file that contains the settings for EF. 如果您的解决方案有多个项目,请尝试将解决方案的启动项目设置为包含Web.Config或App.Config文件的项目,该文件包含EF的设置。

I had a solution with a Web project and seperate project (Data.Models) for my models. 我有一个Web项目的解决方案和我的模型的单独项目(Data.Models)。 All was well until I added a console application to the solution. 一切顺利,直到我向解决方案添加了一个控制台应用程序。 As soon as I set that to be the startup project, EF Migrations would fail. 一旦我将其设置为启动项目,EF迁移就会失败。

Also, if multiple projects in the solution have migrations enabled, always run the Update-Database on each project after you do a Add-Migration. 此外,如果解决方案中的多个项目启用了迁移,则在执行添加迁移后,始终在每个项目上运行Update-Database。 My web project had a pending migration, and the migrations failed weirdly on the second project because of the pending migration in the first. 我的Web项目有一个待定的迁移,由于第一个项目中的挂起迁移,第二个项目上的迁移失败了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个应用程序突然无法再连接到数据库。 其他应用程序连接正常 - One application suddenly cannot connect to database anymore. Other applications connect just fine EF Core 数据库迁移 - EF Core database migrations EF Codefirst迁移新数据库 - EF Codefirst Migrations new database EF迁移不会自动更新数据库 - EF migrations not updating the database automatically 如何在运行时读取迁移文件并将它们应用到 EF Core 中的数据库? - How can I read migrations files at runtime and apply them to my database in EF Core? EF Core 上下文是否在运行时保存所有数据库值? - Does EF Core context hold all database values during runtime? 无法从 Visual Studio 社区 2019 连接到 localhost postgresql db,但可以从 pycharm 连接得很好 - Cannot connect to localhost postgresql db from visual studio community 2019 but can connect just fine from pycharm EF迁移和时间戳列无法更新/运行 - EF migrations and timestamp column, cannot update/run EF迁移:参数&#39;frameworkName&#39;不能是空字符串 - EF Migrations: The parameter 'frameworkName' cannot be an empty string EF Core 迁移:删除迁移并重新同步数据库 - EF Core Migrations: Remove Migrations and Re-Sync Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM