ASP. net mvc Tip #23-Use poco linq to SQL entities

Source: Internet
Author: User

ASP. net mvc Tip #23-Use poco linq to SQL entities
ASP. net mvc Tip #23-Use poco linq to SQL entities

Original American: http://weblogs.asp.net/stephenwalther/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx
Http://www.cnblogs.com/mike108mvp

Note: The translation level is limited. If there are any mistakes or mistakes in the translation, you are welcome to criticize and correct them. Thank you.

Note: ASP. net mvc qq chat group 1215279 welcome to friends interested in ASP. NET MVC

In this post, I will demonstrate how to create a LINQ to SQL entity that does not contain any special features. I will show you how to map a LINQ to SQL entity to a database object using an external XML file.

In this tip, I demonstrate how you can create LINQ to SQL entities that do not contain in any special attributes. I show you how you can use an external XML file to map LINQ to SQL entities to database objects.

I have talked with some people recently, and they are all troubled by the inclusion of the LINQ to SQL class generated by the Visual Studio object relationship designer. They want to use the object relationship designer to generate object classes, but they do not want the generated object classes to be decorated by a bunch of features.

I 've talked to several people recently who are deeply bothered by the fact that the LINQ to SQL classes generated by the Visual Studio Object Relational designer contain attributes. they want to take advantage of the Object Relational designer to generate their entity classes. however, they don't like the fact that the generated entities are decorated with a bunch of attributes.

For example, if you use the object relationship designer to generate the LINQ to SQL class corresponding to the movies database table, you will get the class in Listing 1. The generated class is located in the movies. Designer. CS file.

For example, if you use the object relational designer to generate a LINQ to SQL class that corresponds to the movies database table, then you get the class in Listing 1. this class is generated in the movies. designer. CS file.

Listing 1-movie class (abbreviated)

ThisCodeInconvenience paste, please see Original: http://weblogs.asp.net/stephenwalther/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx

Note: This class in Listing 1 contains the [Table] and [column] features. Use these features to map classes and attributes into tables and table fields of a database.

Notice that the class in Listing 1 has des both [Table] and [column] attributes. LINQ to SQL uses these attributes to map classes and properties to database tables and database table columns.

Some people are troubled by these features. They don't want to mix their persistence logic with domain entities. They want to Use POCO objects (plain old CLR objects) for their entities ).

Some people are disturbed by these attributes. They don't want to mix their persistence logic with their domain entities. They want to Use POCO objects (plain old CLR objects) for their entities.

Fortunately, LINQ to SQL supports two methods for ing classes to database objects. Instead of attributemappingsource, you can use xmlmappingsource. When you use xmlmappingsource, you use an external XML file to map classes to database objects.

Fortunately, LINQ to SQL supports two methods of mapping classes to database objects. instead of using the default attributemappingsource, you can use the xmlmappingsource. when you use the xmlmappingsource, you use an external XML file to map classes to database objects.

You can manually create an xmlfile and use the sqlmetal.exe command line tool to create it. You can run it from the Visual Studio command prompt (command prompt) (Start Menu-> AllProgram-> Visual Studio 2008-> Visual Studio Tools-> Visual Studio 2008 command prompt ).

You can create the XML file by hand or you can use the sqlmetal.exe command line tool. you run sqlmetal.exe from the Visual Studio command prompt (start, all programs, Microsoft Visual Studio 2008, Visual Studio Tools, Visual Studio 2008 command prompt ).

This shows how to use sqlmetal.exe to create an XML ing file from the SQL express database moviesdb. MDF.

Here's how you use sqlmetal.exe to create an XML mapping file from a ranu SQL express database named moviesdb. MDF:

1. navigate to the directory that contains the moviesdb. MDF database file.
1. navigate to the folder containing the moviesdb. MDF Database

2. Execute the following command:
2. Execute the following command:

Sqlmetal/dbml: Movies. dbml moviesdb. MDF

3. Execute the following command:
3. Execute the following command:

Sqlmetal/code: Movies. CS/map: Movies. Map movies. dbml

After executing these commands, you will get three files:
After you execute these commands, you will end up with three files:

· Movies. dbml-movies database Tag file
· Movies. dbml-The movies database markup File

· Movies. CS-movie class corresponding to database objects
· Movies. CS-the movie classes that correspond to the database objects

· Movies. Map-XML file for ing classes and database objects
· Movies. Map-the XML map file that maps the classes to the database objects

After generating these files, you can add the movies. CS and movies. Map files to the models directory of your ASP. net mvc application.

After you generate these files, you can add the movies. CS and movies. map file to your ASP. net mvc application's models folder.

Listing 2 contains the C # movie class file in movies. CS. This file in Listing 2 is almost the same as the file in Listing 1, except that it does not contain special features. The movie class in Listing 2 is a poco object.

The C # movie class file from the movies. CS is contained in Listing 2. the file in Listing 2 is almost exactly the same as the file in Listing 1 blocks t for the fact that the file does not contain any special attributes. the movie class in Listing 2 is a poco object.

Listing 2-movie class (abbreviated)

This code is inconvenient to paste, see the original article: http://weblogs.asp.net/stephenwalther/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx

The file in listing 3 contains the XML ing file generated by sqlmetal.exe. You can create it manually.

The file in listing 3 contains the XML mapping file generated by the sqlmetal.exe tool. You cocould create this file by hand.

Listing 3-movies. Map

This code is inconvenient to paste, see the original article: http://weblogs.asp.net/stephenwalther/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx

After creating an external XML ing file, you must pass the ing file to the datacontext object when initializing datacontext. For example, the Controller in Listing 4 uses the movies. map file in the index () method.

After you create an external XML mapping file, you must pass the mapping file to a datacontext object when you initialize the datacontext. for example, the Controller in Listing 4 uses the movies. map file within its index () method.

Listing 4-homecontroller. CS

This code is inconvenient to paste, see the original article: http://weblogs.asp.net/stephenwalther/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx

The index () method first obtains the connection string from web. config, and then loads the XML ing file from the models directory. When datacontext is created, the connection string and ing source are passed to the datacontext constructor. Finally, a batch of movies records are retrieved from the database and sent to the view.

The index () starts by retrieving a connection string from the Web configuration file. next, the XML Mapping File is loaded from the models folder. the connection string and mapping source are passed to the datacontext constructor when the datacontext is created. finally, a list of movies is retrieved from the database and sent to the view.

The focus of this post is to demonstrate the use of an external XML file instead of using the special LINQ to SQL class to map the class to the database object. Some people don't want to use database persistence logic to dirty their classes. LINQ to SQL is flexible enough to make these people happy.

The point of this tip was to demonstrate that you can use an external XML file instead of attributes within your LINQ to SQL classes to map your classes to your database objects. some people don't want to dirty their classes with database persistence logic. LINQ to SQL is flexible enough to make these people happy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.