如何根据数据库结构的变化更新DataContext? [英] How to update DataContext based on changes in the database structure?

查看:58
本文介绍了如何根据数据库结构的变化更新DataContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用linq-to-sql的DataContext在Visual Studio 2010中工作,它具有到数据库中表的多个映射.现在,当我对数据库的结构进行更改时,我注意到DataContext不变,并导致错误. DataContext不再与数据库的结构相对应.我通常通过删除DataContext中的所有表映射并将其从Visual Studio的数据库资源管理器中再次拖放来解决此问题.我只是觉得这很麻烦,必须有更好的方法来做到这一点?更改数据库结构时是否有按钮或选项可以自动更新DataContext?

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模型,它们就会与数据源断开连接.仅在拖动&从数据源资源管理器中删除已建立连接并查询数据库架构的项目.如果您的模式更改很小(即,一个新的表列),那么手动添加这些更改就很容易了.要进行更剧烈的模式更改,您当前的方法可能是最快,最简单的方法.

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.

使用 sqlmetal.exe 命令行工具.过去我一直在处理数据库架构不断变化的项目,并且在每次构建之前我们都调用sqlmetal,因此在更改时会出现有用的编译错误.如果您的架构变化不大,则可以在项目中简单地添加一个批处理文件,以在必要时更新Linq2Sql模型.

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.

这篇关于如何根据数据库结构的变化更新DataContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆