Search code examples
c#sql-servervisual-studio-2012entity-framework-6entity-framework-migrations

update the database from package manager console in code first environment


Code First Environment

I'm trying to update the database from package Manager console. If my domain class changes, I have to drop and create the database. Instead of dropping the database, how can I update the database?

Commands

  1. By using this command, I installed the Entity Framework successfully.

    PM> Install-Package EntityFramework
    
  2. By using this command, it created the Migration file in my project.

    PM> Enable-Migrations
    
  3. By using this command, I may update the table but I have a problem here.

    PM> Update-Database
    

Error

Specify the '-Verbose' flag to view the SQL statements being applied to the target database. System.Data.SqlClient.SqlException (0x80131904): 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.

Doubt is here

Sometimes it may update if only one field changes in POCO Class. For example I have updated the more number of Domain class. How can I update the database from Package manager Console?


Solution

  • You can specify connection string via ConnectionString parameter:

    Update-Database -ConnectionString "data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose
    

    Also you need to use this parameter with the same value for Add-Migration command:

    Add-Migration Version_Name -ConnectionString "data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose