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

update the database from package manager console in code first environment(在代码优先环境中从包管理器控制台更新数据库)
本文介绍了在代码优先环境中从包管理器控制台更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码优先环境

我正在尝试从包管理器控制台更新数据库.如果我的域类发生更改,我必须删除并创建数据库,而不是删除数据库如何更新数据库.

我已经尝试通过阅读谷歌的一些博客.

命令

PM>安装包EntityFramework

  1. 通过使用此命令,我成功安装了实体框架.

     PM>启用迁移

  2. 通过使用此命令,它在我的项目中创建了迁移文件.

     PM>更新数据库

  3. 通过使用这个命令,我可以更新表格,但是我有一个问题.

错误

<块引用>

指定-Verbose"标志以查看应用于目标数据库的 SQL 语句.System.Data.SqlClient.SqlException (0x80131904):建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确,并且 SQL Server 已配置为允许远程连接.

疑点来了

有时如果POCO类中只有一个字段发生变化,它可能会更新.例如我已经更新了更多的域类,我如何从包管理器控制台更新数据库.有什么想法吗?

解决方案

可以通过ConnectionString参数指定连接字符串:

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

您还需要将此参数与 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

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 ?

解决方案

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

这篇关于在代码优先环境中从包管理器控制台更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)