光脚丫学LINQ(023):使用SQLMetal将对象模型生成为外部映射文件
视频演示:http://u.115.com/file/f22ca6511a
前面我们已经初步的涉及到了如何创建对象模型。从这个演示开始,将会使用一些演示专门来看看有关创建对象模型方面的知识。既然是专门来看,所以所要追究的知识点,自然也就包含的比较多了,当然,一些应该追究的细节问题自然也是不能放过的。
作为基于属性的映射的替代方法,可以使用SQLMetal命令行工具将您的对象模型生成为外部XML文件。 有关更多信息,请参见SqlMetal.exe(代码生成工具)。 使用外部XML映射文件 可以降低代码中的混乱程度。 您还可以通过修改该外部文件来更改行为,而无需重新编译应用程序的二进制文件。 有关更多信息,请参见外部映射引用 (LINQ to SQL)。
要想使用SQLMetal生成指定数据库的外部映射文件,可以使用如下的命令:
SqlMetal /server:(Local)\SQLExpress /database:Northwind /map:"C:\LINQ\ExternalFile.xml" /code:"C:\LINQ\Northwind.cs" SqlMetal /server:(Local)\SQLExpress /database:Northwind /map:"C:\LINQ\ExternalFile.xml" /code:"C:\LINQ\Northwind.cs"
下面是将执行上面的SqlMetal命令之后所生成的映射文件,我干脆全部贴上来吧!
另外,以上的命令不仅仅生成了映射文件,而且也同时生成对象模型的代码文件,也就是那个Northwind.cs文件。
<?xml version="1.0" encoding="utf-8"?> <Database Name="Northwind" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007"> <Table Name="dbo.Categories" Member="Categories"> <Type Name="Categories"> <Column Name="CategoryID" Member="CategoryID" Storage="_CategoryID" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert" /> <Column Name="CategoryName" Member="CategoryName" Storage="_CategoryName" DbType="NVarChar(15) NOT NULL" CanBeNull="false" /> <Column Name="Description" Member="Description" Storage="_Description" DbType="NText" UpdateCheck="Never" /> <Column Name="Picture" Member="Picture" Storage="_Picture" DbType="Image" UpdateCheck="Never" /> <Association Name="FK_Products_Categories" Member="Products" Storage="_Products" ThisKey="CategoryID" OtherKey="CategoryID" DeleteRule="NO ACTION" /> </Type> </Table> <Table Name="dbo.CustomerCustomerDemo" Member="CustomerCustomerDemo"> <Type Name="CustomerCustomerDemo"> <Column Name="CustomerID" Member="CustomerID" Storage="_CustomerID" DbType="NChar(5) NOT NULL" CanBeNull="false" IsPrimaryKey="true" /> <Column Name="CustomerTypeID" Member="CustomerTypeID" Storage="_CustomerTypeID" DbType="NChar(10) NOT NULL" CanBeNull="false" IsPrimaryKey="true" /> <Association Name="FK_CustomerCustomerDemo" Member="CustomerDemographics" Storage="_CustomerDemographics" ThisKey="CustomerTypeID" OtherKey="CustomerTypeID" IsForeignKey="true" /> <Association Name="FK_CustomerCustomerDemo_Customers" Member="Customers" Storage="_Customers" ThisKey="CustomerID" OtherKey="CustomerID" IsForeignKey="true" /> </Type> </Table> <Table Name="dbo.CustomerDemographics" Member="CustomerDemographics"> <Type Name="CustomerDemographics"> <Column Name="CustomerTypeID" Member="CustomerTypeID" Storage="_CustomerTypeID" DbType="NChar(10) NOT NULL" CanBeNull="false" IsPrimaryKey="true" /> <Column Name="CustomerDesc" Member="CustomerDesc" Storage="_CustomerDesc" DbType="NText" UpdateCheck="Never" /> <Association Name="FK_CustomerCustomerDemo" Member="CustomerCustomerDemo" Storage="_CustomerCustomerDemo" ThisKey="CustomerTypeID" OtherKey="CustomerTypeID" DeleteRule="NO ACTION" /> </Type> </Table> <Table Name="dbo.Customers" Member="Customers"> <Type Name="Customers"> <Column Name="CustomerID" Member="CustomerID" Storage="_CustomerID" DbType="NChar(5) NOT NULL" CanBeNull="false" IsPrimaryKey="true" /> <Column Name="CompanyName" Member="CompanyName" Storage="_CompanyName" DbType="NVarChar(40) NOT NULL" CanBeNull="false" /> <Column Name="ContactName" Member="ContactName" Storage="_ContactName" DbType="NVarChar(30)" /> <Column Name="ContactTitle" Member="ContactTitle" Storage="_ContactTitle" DbType="NVarChar(30)" /> <Column Name="Address" Member="Address" Storage="_Address" DbType="NVarChar(60)" /> <Column Name="City" Member="City" Storage="_City" DbType="NVarChar(15)" /> <Column Name="Region" Member="Region" Storage="_Region" DbType="NVarChar(15)" /> <Column Name="PostalCode" Member="PostalCode" Storage="_PostalCode" DbType="NVarChar(10)" /> <Column Name="Country" Member="Country" Storage="_Country" DbType="NVarChar(15)" /> <Column Name="Phone" Member="Phone" Storage="_Phone" DbType="NVarChar(24)" /> <Column Name="Fax" Member="Fax" Storage="_Fax" DbType="NVarChar(24)" /> <Association Name="FK_CustomerCustomerDemo_Customers" Member="CustomerCustomerDemo" Storage="_CustomerCustomerDemo" ThisKey="CustomerID" OtherKey="CustomerID" DeleteRule="NO ACTION" /> <Association Name="FK_Orders_Customers" Member="Orders" Storage="_Orders" ThisKey="CustomerID" OtherKey="CustomerID" DeleteRule="NO ACTION" /> </Type> </Table> <Table Name="dbo.Employees" Member="Employees"> <Type Name="Employees"> <Column Name="EmployeeID" Member="EmployeeID" Storage="_EmployeeID" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert" /> <Column Name="LastName" Member="LastName" Storage="_LastName" DbType="NVarChar(20) NOT NULL" CanBeNull="false" /> <Column Name="FirstName" Member="FirstName" Storage="_FirstName" DbType="NVarChar(10) NOT NULL" CanBeNull="false" /> <Column Name="Title" Member="Title" Storage="_Title" DbType="NVarChar(30)" /> <Column Name="TitleOfCourtesy" Member="TitleOfCourtesy" Storage="_TitleOfCourtesy" DbType="NVarChar(25)" /> <Column Name="BirthDate" Member="BirthDate" Storage="_BirthDate" DbType="DateTime" /> <Column Name="HireDate" Member="HireDate" Storage="_HireDate" DbType="DateTime" /> <Column Name="Address" Member="Address" Storage="_Address" DbType="NVarChar(60)" /> <Column Name="City" Member="City" Storage="_City" DbType="NVarChar(15)" /> <Column Name="Region" Member="Region" Storage="_Region" DbType="NVarChar(15)" /> <Column Name="PostalCode" Member="PostalCode" Storage="_PostalCode" DbType="NVarChar(10)" /> <Column Name="Country" Member="Country" Storage="_Country" DbType="NVarChar(15)" /> <Column Name="HomePhone" Member="HomePhone" Storage="_HomePhone" DbType="NVarChar(24)" /> <Column Name="Extension" Member="Extension" Storage="_Extension" DbType="NVarChar(4)" /> <Column Name="Photo" Member="Photo" Storage="_Photo" DbType="Image" UpdateCheck="Never" /> <Column Name="Notes" Member="Notes" Storage="_Notes" DbType="NText" UpdateCheck="Never" /> <Column Name="ReportsTo" Member="ReportsTo" Storage="_ReportsTo" DbType="Int" /> <Column Name="PhotoPath" Member="PhotoPath" Storage="_PhotoPath" DbType="NVarChar(255)" /> <Association Name="FK_Employees_Employees" Member="ReportsToEmployees" Storage="_ReportsToEmployees" ThisKey="ReportsTo" OtherKey="EmployeeID" IsForeignKey="true" /> <Association Name="FK_Employees_Employees" Member="Employee" Storage="_Employee" ThisKey="EmployeeID" OtherKey="ReportsTo" DeleteRule="NO ACTION" /> <Association Name="FK_EmployeeTerritories_Employees" Member="EmployeeTerritories" Storage="_EmployeeTerritories" ThisKey="EmployeeID" OtherKey="EmployeeID" DeleteRule="NO ACTION" /> <Association Name="FK_Orders_Employees" Member="Orders" Storage="_Orders" ThisKey="EmployeeID" OtherKey="EmployeeID" DeleteRule="NO ACTION" /> </Type> </Table> <Table Name="dbo.EmployeeTerritories" Member="EmployeeTerritories"> <Type Name="EmployeeTerritories"> <Column Name="EmployeeID" Member="EmployeeID" Storage="_EmployeeID" DbType="Int NOT NULL" IsPrimaryKey="true" /> <Column Name="TerritoryID" Member="TerritoryID" Storage="_TerritoryID" DbType="NVarChar(20) NOT NULL" CanBeNull="false" IsPrimaryKey="true" /> <Association Name="FK_EmployeeTerritories_Employees" Member="Employees" Storage="_Employees" ThisKey="EmployeeID" OtherKey="EmployeeID" IsForeignKey="true" /> <Association Name="FK_EmployeeTerritories_Territories" Member="Territories" Storage="_Territories" ThisKey="TerritoryID" OtherKey="TerritoryID" IsForeignKey="true" /> </Type> </Table> <Table Name="dbo.Order Details" Member="OrderDetails"> <Type Name="OrderDetails"> <Column Name="OrderID" Member="OrderID" Storage="_OrderID" DbType="Int NOT NULL" IsPrimaryKey="true" /> <Column Name="ProductID" Member="ProductID" Storage="_ProductID" DbType="Int NOT NULL" IsPrimaryKey="true" /> <Column Name="UnitPrice" Member="UnitPrice" Storage="_UnitPrice" DbType="Money NOT NULL" /> <Column Name="Quantity" Member="Quantity" Storage="_Quantity" DbType="SmallInt NOT NULL" /> <Column Name="Discount" Member="Discount" Storage="_Discount" DbType="Real NOT NULL" /> <Association Name="FK_Order_Details_Orders" Member="Orders" Storage="_Orders" ThisKey="OrderID" OtherKey="OrderID" IsForeignKey="true" /> <Association Name="FK_Order_Details_Products" Member="Products" Storage="_Products" ThisKey="ProductID" OtherKey="ProductID" IsForeignKey="true" /> </Type> </Table> <Table Name="dbo.Orders" Member="Orders"> <Type Name="Orders"> <Column Name="OrderID" Member="OrderID" Storage="_OrderID" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert" /> <Column Name="CustomerID" Member="CustomerID" Storage="_CustomerID" DbType="NChar(5)" /> <Column Name="EmployeeID" Member="EmployeeID" Storage="_EmployeeID" DbType="Int" /> <Column Name="OrderDate" Member="OrderDate" Storage="_OrderDate" DbType="DateTime" /> <Column Name="RequiredDate" Member="RequiredDate" Storage="_RequiredDate" DbType="DateTime" /> <Column Name="ShippedDate" Member="ShippedDate" Storage="_ShippedDate" DbType="DateTime" /> <Column Name="ShipVia" Member="ShipVia" Storage="_ShipVia" DbType="Int" /> <Column Name="Freight" Member="Freight" Storage="_Freight" DbType="Money" /> <Column Name="ShipName" Member="ShipName" Storage="_ShipName" DbType="NVarChar(40)" /> <Column Name="ShipAddress" Member="ShipAddress" Storage="_ShipAddress" DbType="NVarChar(60)" /> <Column Name="ShipCity" Member="ShipCity" Storage="_ShipCity" DbType="NVarChar(15)" /> <Column Name="ShipRegion" Member="ShipRegion" Storage="_ShipRegion" DbType="NVarChar(15)" /> <Column Name="ShipPostalCode" Member="ShipPostalCode" Storage="_ShipPostalCode" DbType="NVarChar(10)" /> <Column Name="ShipCountry" Member="ShipCountry" Storage="_ShipCountry" DbType="NVarChar(15)" /> <Association Name="FK_Order_Details_Orders" Member="OrderDetails" Storage="_OrderDetails" ThisKey="OrderID" OtherKey="OrderID" DeleteRule="NO ACTION" /> <Association Name="FK_Orders_Customers" Member="Customers" Storage="_Customers" ThisKey="CustomerID" OtherKey="CustomerID" IsForeignKey="true" /> <Association Name="FK_Orders_Employees" Member="Employees" Storage="_Employees" ThisKey="EmployeeID" OtherKey="EmployeeID" IsForeignKey="true" /> <Association Name="FK_Orders_Shippers" Member="Shippers" Storage="_Shippers" ThisKey="ShipVia" OtherKey="ShipperID" IsForeignKey="true" /> </Type> </Table> <Table Name="dbo.Products" Member="Products"> <Type Name="Products"> <Column Name="ProductID" Member="ProductID" Storage="_ProductID" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert" /> <Column Name="ProductName" Member="ProductName" Storage="_ProductName" DbType="NVarChar(40) NOT NULL" CanBeNull="false" /> <Column Name="SupplierID" Member="SupplierID" Storage="_SupplierID" DbType="Int" /> <Column Name="CategoryID" Member="CategoryID" Storage="_CategoryID" DbType="Int" /> <Column Name="QuantityPerUnit" Member="QuantityPerUnit" Storage="_QuantityPerUnit" DbType="NVarChar(20)" /> <Column Name="UnitPrice" Member="UnitPrice" Storage="_UnitPrice" DbType="Money" /> <Column Name="UnitsInStock" Member="UnitsInStock" Storage="_UnitsInStock" DbType="SmallInt" /> <Column Name="UnitsOnOrder" Member="UnitsOnOrder" Storage="_UnitsOnOrder" DbType="SmallInt" /> <Column Name="ReorderLevel" Member="ReorderLevel" Storage="_ReorderLevel" DbType="SmallInt" /> <Column Name="Discontinued" Member="Discontinued" Storage="_Discontinued" DbType="Bit NOT NULL" /> <Association Name="FK_Order_Details_Products" Member="OrderDetails" Storage="_OrderDetails" ThisKey="ProductID" OtherKey="ProductID" DeleteRule="NO ACTION" /> <Association Name="FK_Products_Categories" Member="Categories" Storage="_Categories" ThisKey="CategoryID" OtherKey="CategoryID" IsForeignKey="true" /> <Association Name="FK_Products_Suppliers" Member="Suppliers" Storage="_Suppliers" ThisKey="SupplierID" OtherKey="SupplierID" IsForeignKey="true" /> </Type> </Table> <Table Name="dbo.Region" Member="Region"> <Type Name="Region"> <Column Name="RegionID" Member="RegionID" Storage="_RegionID" DbType="Int NOT NULL" IsPrimaryKey="true" /> <Column Name="RegionDescription" Member="RegionDescription" Storage="_RegionDescription" DbType="NChar(50) NOT NULL" CanBeNull="false" /> <Association Name="FK_Territories_Region" Member="Territories" Storage="_Territories" ThisKey="RegionID" OtherKey="RegionID" DeleteRule="NO ACTION" /> </Type> </Table> <Table Name="dbo.Shippers" Member="Shippers"> <Type Name="Shippers"> <Column Name="ShipperID" Member="ShipperID" Storage="_ShipperID" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert" /> <Column Name="CompanyName" Member="CompanyName" Storage="_CompanyName" DbType="NVarChar(40) NOT NULL" CanBeNull="false" /> <Column Name="Phone" Member="Phone" Storage="_Phone" DbType="NVarChar(24)" /> <Association Name="FK_Orders_Shippers" Member="Orders" Storage="_Orders" ThisKey="ShipperID" OtherKey="ShipVia" DeleteRule="NO ACTION" /> </Type> </Table> <Table Name="dbo.Suppliers" Member="Suppliers"> <Type Name="Suppliers"> <Column Name="SupplierID" Member="SupplierID" Storage="_SupplierID" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert" /> <Column Name="CompanyName" Member="CompanyName" Storage="_CompanyName" DbType="NVarChar(40) NOT NULL" CanBeNull="false" /> <Column Name="ContactName" Member="ContactName" Storage="_ContactName" DbType="NVarChar(30)" /> <Column Name="ContactTitle" Member="ContactTitle" Storage="_ContactTitle" DbType="NVarChar(30)" /> <Column Name="Address" Member="Address" Storage="_Address" DbType="NVarChar(60)" /> <Column Name="City" Member="City" Storage="_City" DbType="NVarChar(15)" /> <Column Name="Region" Member="Region" Storage="_Region" DbType="NVarChar(15)" /> <Column Name="PostalCode" Member="PostalCode" Storage="_PostalCode" DbType="NVarChar(10)" /> <Column Name="Country" Member="Country" Storage="_Country" DbType="NVarChar(15)" /> <Column Name="Phone" Member="Phone" Storage="_Phone" DbType="NVarChar(24)" /> <Column Name="Fax" Member="Fax" Storage="_Fax" DbType="NVarChar(24)" /> <Column Name="HomePage" Member="HomePage" Storage="_HomePage" DbType="NText" UpdateCheck="Never" /> <Association Name="FK_Products_Suppliers" Member="Products" Storage="_Products" ThisKey="SupplierID" OtherKey="SupplierID" DeleteRule="NO ACTION" /> </Type> </Table> <Table Name="dbo.Territories" Member="Territories"> <Type Name="Territories"> <Column Name="TerritoryID" Member="TerritoryID" Storage="_TerritoryID" DbType="NVarChar(20) NOT NULL" CanBeNull="false" IsPrimaryKey="true" /> <Column Name="TerritoryDescription" Member="TerritoryDescription" Storage="_TerritoryDescription" DbType="NChar(50) NOT NULL" CanBeNull="false" /> <Column Name="RegionID" Member="RegionID" Storage="_RegionID" DbType="Int NOT NULL" /> <Association Name="FK_EmployeeTerritories_Territories" Member="EmployeeTerritories" Storage="_EmployeeTerritories" ThisKey="TerritoryID" OtherKey="TerritoryID" DeleteRule="NO ACTION" /> <Association Name="FK_Territories_Region" Member="Region" Storage="_Region" ThisKey="RegionID" OtherKey="RegionID" IsForeignKey="true" /> </Type> </Table> </Database>