Barefoot learning LINQ (018): creating and using custom data context

Source: Internet
Author: User

Http://u.115.com/file/f233b821ab demo

It is much easier to start with the strongly typed view of the database. By strongly classifying datacontext objects, you do not need to call gettable. When you use a strongly typed datacontext object, you can use strongly typed tables in all queries.
In the following steps, you create a MERs table as a strongly typed table mapped to the customers table in the database.

Strongly typed datacontext objects

public class Northwind : DataContext   {       // Table<T> abstracts database details per table/data type.       public Table<Customer> Customers;       public Table<Order> Orders;         public Northwind(string connection) : base(connection) { }   }  public class Northwind : DataContext{    // Table<T> abstracts database details per table/data type.    public Table<Customer> Customers;    public Table<Order> Orders;    public Northwind(string connection) : base(connection) { }}

Then you can use the following method to use custom strong data context, instead of the built-in datacontext.

// Use a connection string.   Northwind db = new Northwind(@"C:\linqtest5\Northwind.mdf");     // Query for customers from Seattle.    var SeattleCustomers =       from CustomerObject in db.Customers       where CustomerObject.City == "Seattle"      select CustomerObject;     foreach (var CustomerObject in SeattleCustomers)   {       Console.WriteLine("ID={0}", CustomerObject.CustomerID);   }     // Freeze the console window.   Console.ReadLine(); 

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.