首页 / 软件开发 / LINQ / 学Linq to sql(九):其它补充
学Linq to sql(九):其它补充2010-11-04 博客园 lovecherry外部映射文件我们可以使用sqlmetal命令行工具来生成外部映射文件,使用方法如下:1、开始菜单 -》 VS2008 -》VS工具 -》VS2008命令行提示2、输入命令:D:Program FilesMicrosoft Visual Studio 9.0VC>sqlmetal /conn:server=xxx;
database=Northwind;uid=xxx;pwd=xxx /map:c:
orthwind.map /code:c:
orthwind.cs
3、这样,我们就可以在C盘下得到一个xml映射文件和C#的实体类代码4、把.cs文件添加到项目中来(放到App_Code目录),然后使用下面的代码加载映射文件:String path = @"C:Northwind.map";
XmlMappingSource xms = XmlMappingSource.FromXml(File.ReadAllText(path));
Northwind ctx = new Northwind("server=xxx;database=Northwind;uid=xxx;pwd=xxx", xms);
5、现在就可以照常进行其它工作了。使用sqlmetal可以很方便的同步与实体和映射文件。每次修改数据库结构,从dbml设计器上删除表、存储过程然后再重新添加也是很麻烦的事情。处理空值var count = (from c in ctx.Customers where c.Region == null select c).Count();
Response.Write(count + "<br/>");
var query = from emp in ctx.Employees select emp.ReportsTo;
foreach (Nullable<int> r in query)
{
Response.Write(r.HasValue ? r.Value.ToString() + "<br/>" : "没有<br/>");
}
代码执行后捕获到下面的SQL被执行:SELECT COUNT(*) AS [value]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[Region] IS NULL
SELECT [t0].[ReportsTo]
FROM [dbo].[Employees] AS [t0]