Get the App
SLTechnology News&Howtos  ›  Development  › 

How Linq uses sqlmetal

Shulou Source: shulou.com Published: 2022-06-02 18:38:52 09月22日 Update

This article is about how Linq uses sqlmetal. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

First of all, let you know about the external mapping file, and then give a comprehensive introduction to Linq's use of sqlmetal.

External mapping file

We can Linq use the sqlmetal command line tool to generate an external mapping file, using the following methods:

1. Start menu-"VS2008 -" VS tool-"VS2008 command line prompt"

2. Enter the command:

D:\ Program Files\ Microsoft Visual Studio 9.0\ VC > sqlmetal / conn:server=xxx; database=Northwind;uid=xxx;pwd=xxx / map:c:\ northwind.map / code:c:\ northwind.cs

3. In this way, we can get a xml mapping file and the entity class code of C # under C disk.

Add the .cs file to the project (put it in the App_Code directory), and then load the mapping file using the following 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. Now you can do other work as usual. Linq uses sqlmetal to easily synchronize databases with entities and mapping files. It is also troublesome to delete tables, stored procedures, and then re-add tables from the dbml designer every time you modify the database structure.

Deal with null values

Var count = (from c in ctx.Customers where c.Region = = null select c). Count (); Response.Write (count + ""); var query = from emp in ctx.Employees select emp.ReportsTo; foreach (Nullable r in query) {Response.Write (r.HasValue? R.Value.ToString () + "": "No");}

After code execution, the following SQL is captured and executed:

SELECT COUNT (*) AS [value] FROM [dbo]. [Customers] AS [T0] WHERE [T0]. [Region] IS NULL SELECT [T0]. [ReportsTo] FROM [dbo]. [Employees] AS [T0] Thank you for reading! This is the end of this article on "how Linq uses sqlmetal". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Files code commands content entities tools data databases more articles facets good practical things usage articles methods directories see knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Docker MariaDB Microsoft