简体   繁体   English

在代码优先环境中从包管理器控制台更新数据库

[英]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. 通过使用此命令,我成功安装了Entity Framework。

      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. 指定'-Verbose'标志以查看应用于目标数据库的SQL语句。 System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. System.Data.SqlClient.SqlException(0x80131904):建立与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是否配置为允许远程连接。

Doubt is here 怀疑在这里

Sometimes It may update if only one field changes in POCO Class. 有时如果POCO类中只有一个字段发生变化,它可能会更新。 For example I have Updated the more number of Domain class ,How can i Update the Database from Package manager Console. 例如,我已经更新了更多的Domain类 ,如何从Package Manager Console更新数据库。 Any Idea ? 任何想法 ?

You can specify connection string via ConnectionString parameter: 您可以通过ConnectionString参数指定连接字符串:

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命令的值相同:

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

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. 我认为这是原型制作时最懒的选择。

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. 如果要使用迁移,请将其更改为MigrateDatabaseToLatestVersion。 http://www.codeguru.com/csharp/article.php/c19999/Understanding-Database-Initializers-in-Entity-Framework-Code-First.htm 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. 虽然您可以将其添加到Add-Migration和Update-Migration,但我可能会在应用程序中修复它。 I set mine in the constructor of my context which points to my config file (web.config for ASP.NET). 我在我的上下文的构造函数中设置了我的指向我的配置文件(ASP.NET的web.config)。

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext()
        : base("MyConnection", throwIfV1Schema: false)
    {
        Database.SetInitializer<ApplicationDbContext>(new MigrateDatabaseToLatestVersion<ApplicationDbContext, MyObjextContextMigration>());
    }
    ...

Where to put the connection string? 在哪里放连接字符串?

You do not need to specify it with every command in the package manager console. 您无需在包管理器控制台中使用每个命令指定它。 You can put it in appsettings.json in the project where your DbContext class (read "from DbContext derived class") resides. 您可以将它放在appbtings.json中,在DbContext类(从“DbContext派生类”中读取)所在的项目中。

{
  "ConnectionStrings": {
    "MyConnectionString": "Server=yourServer;Port=5432;Database=yourDatabase;User Id=yourDatabaseUsername;Password=yourDatabasePassword;"
  }
}

It will be used for migrations. 它将用于迁移。

Important: If you have multiple projects in your solution, you must select the project in the 'Default project'-dropdown (in the Package manager Console) and you must set the project as your startup project (in the Solution Explorer). 要点:如果解决方案中有多个项目,则必须在“默认项目”下拉列表中(在包管理器控制台中)选择项目,并且必须将项目设置为启动项目(在解决方案资源管理器中)。

Failing to do so, might cause the wrong appsettings.json to be used with an incorrect/different connectionstring. 如果不这样做,可能会导致错误的appsettings.json与不正确/不同的连接字符串一起使用。

This was my experience with EF Core 2.1 and probably applies to the other versions of EF. 这是我使用EF Core 2.1的经验,可能适用于其他版本的EF。

您需要更新SSDT转到工具>扩展和更新>更新> SQL服务器数据工具

暂无
暂无

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

相关问题 如何从Visual Studio中的包管理器控制台调用针对SQL Azure的update-database? - How to call update-database from package manager console in Visual Studio against SQL Azure? 使用包管理器控制台“ Update-Database”等在实体框架中以编程方式连接字符串 - Programmatically connection string in entity framework with package manager console “Update-Database” etc 从 package 控制台管理器更新数据库时出现此错误值不能是 null。 (参数“连接字符串”) - While updating database from package console manager getting this error Value cannot be null. (Parameter 'connectionString') 在Visual Studio中的程序包管理器控制台上执行“更新数据库”时,出现错误:找不到内容根文件夹 - When doing an “update-database” on the package manager console in visual studio i get error : Could not find content root folder 如何更新“从数据库优先编码”生成的数据库? - How to update my database generated by “code first from database”? EF代码优先-从Azure门户运行更新数据库 - EF Code First - run Update-Database from Azure portal 从visual studio命令提示符运行nuget包管理器控制台 - Run nuget package manager console from visual studio command prompt 是否可以从 package 管理器控制台运行 angular 命令? - Is it possible to run angular commands from package manager console? 如何使用“数据库优先”更新首先使用EF代码的模型 - How to update model working with EF code first with 'Code First from Database' 检查 DBContext 是否有变化并在锅上生成更新脚本而不是使用包管理器控制台 - Check if there are changes in DBContext and Generate update script on the pot instead of using Package Manager Console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM