简体   繁体   English

如何使Visual Studio自动从SQL Server检索数据库更改?

[英]How to make Visual Studio automatically retrieve database changes from SQL Server?

So, I made a database in SQL Server Express 2014. After that, from Visual Studio 2015 Community, I make a new project, and then I add new data source, and then choosing Database, and then Dataset, and then choosing all tables and views in the database. 因此,我在SQL Server Express 2014中创建了一个数据库。此后,在Visual Studio 2015社区中,创建了一个新项目,然后添加了新的数据源,然后依次选择“数据库”,“数据集”,所有表和数据库中的视图。

The problem is, after I made some changes to the database from the SQL Server (either changing a column name, or column data type, or manually adding some data for testing), the changes are not pushed back to my project in Visual Studio, it also says error table name from the renamed Column Name. 问题是,在我从SQL Server对数据库进行了一些更改(更改列名称或列数据类型,或手动添加一些数据以进行测试)之后,这些更改没有被推回到Visual Studio中的项目中,它还从重命名的列名中说出错误表名。

Any idea how to automatically set the changes made in SQL Server to the Visual Studio side? 知道如何将SQL Server中所做的更改自动设置到Visual Studio端吗?

DataSets in .NET do not automatically sync their schemas with SQL, so changes made in SQL will not be reflected in Visual Studio. .NET中的数据集不会自动将其架构与SQL同步,因此在SQL中所做的更改不会反映在Visual Studio中。 To see the changes in Visual Studio you need to: 要查看Visual Studio中的更改,您需要:

  • open the .xsd file for your dataset in design view by double clicking on it 双击在设计视图中为您的数据集打开.xsd文件
  • then delete the table that has changed from the schema 然后从架构中删除已更改的表
  • then open the server explorer 然后打开服务器资源管理器
  • find your data connection 找到您的数据连接
  • expand the tables node 展开表格节点
  • and drag the table back onto the xsd designer. 并将表格拖回到xsd设计器上。

That's obviously a nuisance to do, so you could use Entity Framework to automate this process. 显然这很麻烦,因此您可以使用Entity Framework来自动执行此过程。

Entity Framework allows you to work in a few different ways: 实体框架允许您以几种不同的方式工作:

  • You can define tables on the fly from your code, and EF can automatically produce migrations to update the database schema to match your changes, eg if you rename a class property in Visual Studio it will change the database schema. 您可以从代码中动态定义表,EF可以自动进行迁移以更新数据库架构以匹配您的更改,例如,如果在Visual Studio中重命名类属性,它将更改数据库架构。 That's called 'code first'. 这就是所谓的“代码优先”。
  • Or, you can design the database and have Entity Framework generate code, which is unsurprisingly called 'database first'. 或者,您可以设计数据库并让Entity Framework生成代码,这毫不奇怪地称为“数据库优先”。

This is the official site for Entity Framework. 这是 Entity Framework 的官方网站

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

相关问题 如何在Visual Studio 2017中从C#代码制作SQL Server 2017本地数据库? - How to make a SQL Server 2017 local database from C# code in Visual Studio 2017? 在Visual Studio中使用SQL Server中的数据库 - Using Database from SQL Server in Visual Studio 如何停止Visual Studio调试,以便不提交sql数据库更改? - How to stop Visual Studio debugging so sql database changes not commit? 从 Visual Studio 2013 数据库项目中清理 SQL Server 数据库 - Clean SQL Server database from Visual Studio 2013 database project 如何在 Visual Studio 2022 中使用已连接的 SQL 服务器数据库进行查询? - How do I make a query using my connected SQL Server Database in Visual Studio 2022? 如何在SQL Server Management Studio中使用Visual Studio创建连接数据库? - How connect database created in SQL Server Management Studio with Visual Studio? 如何从数据库(formview)中检索数据并将其相乘,Visual Studio - how to retrieve data from my database (formview) and multiply it, visual studio 从 Visual Studio 连接到 SQL Server 数据库的问题 - Issue with connecting to SQL Server database from Visual Studio 无法从Visual Studio Express 2012创建SQL Server数据库 - Unable to create SQL Server database from Visual Studio Express 2012 如何在Visual Studio中的其他项目中使用SQL Server数据库项目? - How to use SQL Server Database Project in other Projects in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM