Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update DataContext based on changes in the database structure?

I'm working in Visual Studio 2010 using linq-to-sql's DataContext which has several maps to tables in the database. Now when I change something to the structure of the database I noticed that the DataContext doesn't change and results in errors. The DataContext no longer corresponds with the structure of the database. I usually resolve this issue by deleting all table maps in the DataContext and drag&drop them again from the Database Explorer in Visual Studio. I just feel that this is very cumbersome and that there must be a better way to do this? Is there a button or an option to update the DataContext automatically when I changed the database structure?

like image 544
Bazzz Avatar asked Nov 05 '22 04:11

Bazzz


1 Answers

Linq2Sql models are disconnected from the data source once they have been generated. It's only at the point of dragging & dropping items from the data source explorer that a connection is made and the database schema is queried. If your schema changes are small (ie a new table column) it's easy enough to add these manually. For more drastic schema changes your current method is probably the quickest and easiest.

It is possible to automate this code generation process by using the sqlmetal.exe command line tool. I've worked on projects in the past with database schemas which were constantly changing and we invoked sqlmetal before each build so we got useful compile errors when it changed. If your schema doesn't change so much, you could simply have a batch file in your project to update the Linq2Sql model when necessary.

like image 67
MattDavey Avatar answered Nov 12 '22 16:11

MattDavey