Http://u.115.com/file/f22ca6511a demo
Previously, we have initially discussed how to create an object model. Starting from this demonstration, we will use some demos to look at the knowledge about creating object models. Since it is a special point of view, the knowledge points to be pursued naturally include more. Of course, some details that should be investigated cannot be ignored.
As an alternative to property-based ing, you can use the sqlmetal command line tool to generate your object model as an external XML file. For more information, see sqlmetal.exe (code generation tool ). Using External XML ing files can reduce code confusion. You can also modify the behavior of an external file without re-compiling the binary file of the application. For more information, see external ing reference (LINQ to SQL ).
To use sqlmetal to generate an external ing file for the specified database, run the following command:
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"
The following is the ing file generated after the above sqlmetal command is executed. I will simply paste it all!
In addition, the above commands not only generate a ing file, but also generate a code file for the object model, that is, the northwind. CS file.
<?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>