Error in update-database command in code first migration

后端 未结 10 1531
盖世英雄少女心
盖世英雄少女心 2020-12-15 05:01

I am working on Desktop application in WPF and creating SqlRepository with LocalDB to store data. I am using following tools

  • Visual Studio 20
相关标签:
10条回答
  • 2020-12-15 05:12

    If you are trying to connect to Sql Server instead of localdb then make sure the default connection factory in web.config file is as below:

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.\SQLEXPRESS2012; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
     </defaultConnectionFactory>
    
    0 讨论(0)
  • 2020-12-15 05:13

    A network-related or instance-specific error occurred while establishing a connection to 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. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

    This error message informs you that it is not possible to connect to SQL Server. The possible reasons and their elimination is described below:

    1) SQL Server is not started. Starting of it will allow you to see your SQL Server/instance in the drop-down list of available SQL Servers.

    • Go to the Start menu -> Control Panel -> Administration Tools -> Services.
    • In the list of services find SQL Server (instance name, by default it is EZPARTS5) and check its status, it must be Started
      (if it is not started, then right click on SQL Server and select
      Start from the context menu).

    2) Firewall is blocking port 1433 (MSSQL standard port for connections). It can be disabled following the steps below:

    • Go to the Start menu -> Control Panel -> Administration Tools -> Services.
    • Find Firewall service, it must be disabled (if it is not, then right click the service and select Stop from the context menu).

    Note: More information on this can be found on the official Microsoft site: Link

    3) TCP/IP protocol is disabled for MSSQL protocols. To enable it, see the steps below:

    • Navigate to SQL Server Configuration Manager in the Start menu.
    • Specify settings for TCP/IP protocol in SQL Server Configuration Manager.
    • Restart the computer.

    Note: More information on this can be found on the official Microsoft site: Link

    4) Make sure your database engine is configured to accept remote connections (If you are using centralized database):

    • Open SQL Server Management Studio.
    • Right click SQL Server instance -> Properties -> Connections -> Check the Allow remote connections to this server box.
    • Go to the General section and check name of SQL Server specified in the Name field.

    5) If you are using a named SQL Server instance, make sure you are using that instance name in your connection strings. Usually the format needed to specify the database server is machinename\instancename.

    6) Make sure your login account has access permission on the database you used during login. Alternative: If you still can’t get any connection, you may want to create a SQL account on the server, a corresponding SQL user on the database in question, and just use this username/password login data to connect to SQL Server.

    Referred from this link

    0 讨论(0)
  • 2020-12-15 05:22

    Diego's answer is correct.

    This problem occurs when there is no connection string in project marked as startup project. Then EF tries to connect to some default database engine to perform update. In my case it tried to use express, and for some reason it couldn't connect. And the error was thrown.

    Run your "update-database" with option "-Verbose". One of the lines there shows which StartUp project is used. Check your connection string in this project, or change the startup project to the one that has correct connection string. That solves the problem.

    0 讨论(0)
  • 2020-12-15 05:23

    Try with this Connectionstring:

    <connectionStrings>
      <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=D:\USERS\USERNAME\DOCUMENTS\TestDatabase\testdb.MDF;Initial Catalog=testdb;Integrated Security=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
    
    0 讨论(0)
  • 2020-12-15 05:23

    I had this same problem with a freshly created ASP.NET project.

    First I tried to set the startup project as mentioned above. There was no startup project selected and I was unable to select one.

    Ultimately I updated my Visual Studio from 2015 Update 1 to 2015 Update 3 and

    update-database
    

    executed without problems.

    0 讨论(0)
  • 2020-12-15 05:24

    I had similar problem and fixed it when I changed the "start-up project" from another module to the module containing references to all other projects in the solution. Right-click module >> click "Set as StartUp Project"

    0 讨论(0)
提交回复
热议问题