简体   繁体   中英

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.

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.

Any idea how to automatically set the changes made in SQL Server to the Visual Studio side?

DataSets in .NET do not automatically sync their schemas with SQL, so changes made in SQL will not be reflected in Visual Studio. To see the changes in Visual Studio you need to:

  • open the .xsd file for your dataset in design view by double clicking on it
  • 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.

That's obviously a nuisance to do, so you could use Entity Framework to automate this process.

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. That's called 'code first'.
  • Or, you can design the database and have Entity Framework generate code, which is unsurprisingly called 'database first'.

This is the official site for Entity Framework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM