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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With