Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 change, I have to drop and create the database,Instead of dropping the Database how can i update the database.

I have already try by reading some blogs in google.

commands

PM> Install-Package EntityFramework
  1. By using this command i install the Entity Framework successfully.

     PM> Enable-Migrations
    
  2. By using this command it created the Migration file in my project.

     PM> Update-Database
    
  3. By using this command , i may update the table but i have a Problem here.

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. Any Idea ?

like image 375
vinodh Avatar asked Aug 25 '15 04:08

vinodh


People also ask

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.

How do I update my database in migration?

After creating a migration file using the add-migration command, you have to update the database. Execute the Update-Database command to create or modify a database schema. Use the –verbose option to view the SQL statements being applied to the target database.


3 Answers

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
like image 99
at least 3 and no more than 30 Avatar answered Oct 10 '22 02:10

at least 3 and no more than 30


I used

Enable-Migrations -EnableAutomaticMigrations -Force

then

Update-Database

And this migrated my changes (additional columns) while preserving my test data. I think this is the laziest option while prototyping.

like image 45
toddmo Avatar answered Oct 10 '22 03:10

toddmo


Looks like you have multiple issues. Regarding not wanting to drop and recreate the database, that is determined by your database initializer. If you want to use migrations you change it to MigrateDatabaseToLatestVersion. http://www.codeguru.com/csharp/article.php/c19999/Understanding-Database-Initializers-in-Entity-Framework-Code-First.htm

Second, it doesn't matter how many fields you change, they will be rolled into a single migration based on changes from the last migration.

Third, as the others have pointed out, it seems you have a connection string issue. While you can add that to Add-Migration and Update-Migration I would probably fix it in the application. I set mine in the constructor of my context which points to my config file (web.config for ASP.NET).

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext()
        : base("MyConnection", throwIfV1Schema: false)
    {
        Database.SetInitializer<ApplicationDbContext>(new MigrateDatabaseToLatestVersion<ApplicationDbContext, MyObjextContextMigration>());
    }
    ...
like image 2
Steve Greene Avatar answered Oct 10 '22 04:10

Steve Greene