update the database from package manager console in code first environment(在代码优先环境中从包管理器控制台更新数据库)
问题描述
代码优先环境
我正在尝试从包管理器控制台更新数据库.如果我的域类发生更改,我必须删除并创建数据库,而不是删除数据库如何更新数据库.
我已经尝试通过阅读谷歌的一些博客.
命令
PM>安装包EntityFramework
通过使用此命令,我成功安装了实体框架.
PM>启用迁移
通过使用此命令,它在我的项目中创建了迁移文件.
PM>更新数据库
通过使用这个命令,我可以更新表格,但是我有一个问题.
错误
<块引用>指定-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
By using this command i install the Entity Framework successfully.
PM> Enable-Migrations
By using this command it created the Migration file in my project.
PM> Update-Database
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
这篇关于在代码优先环境中从包管理器控制台更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在代码优先环境中从包管理器控制台更新数据库
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01