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

前端 未结 3 486
天涯浪人
天涯浪人 2021-01-19 00:25

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 structu

3条回答
  •  爱一瞬间的悲伤
    2021-01-19 00:31

    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.

提交回复
热议问题