zoukankan      html  css  js  c++  java
  • 光脚丫学LINQ(031):开篇及一对多映射关系的再学习

    视频演示:http://u.115.com/file/f22c71efbb

    特别说明
    从这个演示开始,将会用好几个演示连续性的介绍有关LINQ to SQL的关系映射的问题。


    演示说明
    此演示简单介绍了《关系映射》系列课程的基本安排,以及简单说明LINQ to SQL支持的三种类型的映射关系:一对一、一对多和多对多。
    然后比较深入的介绍一对多的映射关系,并且借此学习了EntitySetEntityRef
    并介绍了建立映射关系必须的AssociationAttribute修饰属性,简单说明了其Name属性的作用,
    重点说明了Storage属性的作用和ThisKeyOtherKey的作用,以及对此的推测。
    另外也顺带介绍了什么叫做键值属性


    演示重点
    LINQ to SQL支持的三种映射关系:一对一、一对多、多对多(实际上是两个一对多组合而成的)。
    这里最好对EntityRef和EntitySet做一个介绍,可摘录MSDN的说明。
    在一对多的关系映射中,建立关系的两个实体,其中一方可以称之为单一方,而另一方可以称之为集合方。从单一方获取集合放的管理数据的时候,所使用到的属性可以被称之为集合属性,而集合属性的数据类型,以及用来存储集合属性值的私有字段类型,都应该是EntitySet<TEntity>类型的。然而从集合方获取单一方关联对象的时候,其私有字段的类型最好是EntityRef<TEntity>,如果你要使用EntitySet<TEntity>当然也是可以的。其他两种映射关系可以以此类推。
    为建立映射的实体类成员添加AssociationAttribute特性,并且正确设置Storage、ThisKey和OtherKey属性值,就能建立起映射关系了。
    Storage:指定获取的关联数据被存储在什么地方,通常是一个私有字段。
    ThisKey:指定当前实体类的一个键值属性,由此属性值提供要获取的是哪些数据。
    OtherKey:指定建立关系的另一个实体类的一个键值属性,用来指定要比对的是数据表中的哪一个数据列。

    什么是键值属性呢?所谓的键值属性和一般的属性并没有什么区别,只是它映射到数据表的键值列上,比如主键列,或者外键列。


    演示代码
    建立映射关系的两个实体类的代码。

    using System;   
    using System.Collections.Generic;   
    using System.Linq;   
    using System.Text;   
    using System.Data.Linq.Mapping;   
    using System.Data.Linq;   
      
    namespace Demo06Ex01   
    {   
        [Table(Name = "Customers")]   
        public class Customer   
        {   
            private string _CustomerID;   
            private string _ContactName;   
            private string _Phone;   
      
            [Column(Name = "CustomerID", Storage = "_CustomerID",   
                IsPrimaryKey = true)]   
            public string CustomerID   
            {   
                get { return this._CustomerID; }   
                set { this._CustomerID = value; }   
            }   
      
            [Column(Name = "ContactName", Storage = "_ContactName")]   
            public string ContactName   
            {   
                get { return this._ContactName; }   
                set { this._ContactName = value; }   
            }   
      
            [Column(Name = "Phone", Storage = "_Phone")]   
            public string Phone   
            {   
                get { return this._Phone; }   
                set { this._Phone = value; }   
            }   
      
            private EntitySet<Order> _Orders;   
            [Association(Storage = "_Orders", ThisKey = "CustomerID", OtherKey = "CustomerID")]   
            public EntitySet<Order> Orders   
            {   
                get { return this._Orders; }   
                set { this._Orders.Assign(value); }   
            }   
        }   
    } 
    using System;   
    using System.Collections.Generic;   
    using System.Linq;   
    using System.Text;   
    using System.Data.Linq.Mapping;   
    using System.Data.Linq;   
      
    namespace Demo06Ex01   
    {   
        [Table(Name = "Orders")]   
        public class Order   
        {   
            private int _OrderID;   
            private string _CustomerID;   
            private DateTime _OrderDate;   
            private decimal _Freight;   
      
            [Column(Name = "OrderID", Storage = "_OrderID",   
                IsPrimaryKey = true)]   
            public int OrderID   
            {   
                get { return this._OrderID; }   
                set { this._OrderID = value; }   
            }   
      
            [Column(Name = "CustomerID", Storage = "_CustomerID")]   
            public string CustomerID   
            {   
                get { return this._CustomerID; }   
                set { this._CustomerID = value; }   
            }   
      
            [Column(Name = "OrderDate", Storage = "_OrderDate")]   
            public DateTime OrderDate   
            {   
                get { return this._OrderDate; }   
                set { this._OrderDate = value; }   
            }   
      
            [Column(Name = "Freight", Storage = "_Freight")]   
            public decimal Freight   
            {   
                get { return this._Freight; }   
                set { this._Freight = value; }   
            }   
      
            private EntityRef<Customer> _Customer;   
            [Association(Storage = "_Customer", ThisKey = "CustomerID", OtherKey = "CustomerID")]   
            public Customer Customer   
            {   
                get { return this._Customer.Entity; }   
                set { this._Customer.Entity = value; }   
            }   
        }   
    } 
    


    用来测试通过映射的关系获取关联数据的代码
    以下的代码通过客户对象获取客户的所有订单记录。

    // *************************************************   
                // 通过客户对象获取客户的所有订单记录。   
                // *************************************************   
                string DatabaseFileName = @"C:\LINQ\Northwind.mdf";   
                NorthwindDataContext db = new NorthwindDataContext(DatabaseFileName);   
      
                var AllCustomers = from CustomerObject in db.Customers   
                                   select CustomerObject;   
      
                foreach (var CustomerObject in AllCustomers)   
                {   
                    Console.WriteLine("------------------------------------------");   
                    Console.ForegroundColor = ConsoleColor.Green;   
                    Console.WriteLine("Customer ID : {0}", CustomerObject.CustomerID);   
                    Console.ForegroundColor = ConsoleColor.White;   
                    Console.WriteLine("Customer Name : {0}", CustomerObject.ContactName);   
                    Console.WriteLine("Phone : {0}", CustomerObject.Phone);   
      
                    Thread.Sleep(1000);   
      
                    foreach (var OrderObject in CustomerObject.Orders)   
                    {   
                        Console.WriteLine("     OrderID={0}, CustomerID={1}, Freight={2}",   
                            OrderObject.OrderID,   
                            OrderObject.CustomerID,   
                            OrderObject.Freight);   
                    }   
      
                    Thread.Sleep(2000);   
                } 
    


    下面的代码则是通过订单对象获取下订单的客户对象。

    // *************************************************   
                // 通过订单对象获取下订单的客户对象。   
                // *************************************************   
                NorthwindDataContext db = new NorthwindDataContext(@"C:\LINQ\Northwind.mdf");   
                var AllOrders = from OrderObject in db.Orders   
                                select OrderObject;   
                foreach (var OrderObject in AllOrders)   
                {   
                    Console.WriteLine("---------------------");   
                    Console.WriteLine("OrderID={0}, CustomerID={1}, Freight={2}",   
                        OrderObject.OrderID,   
                        OrderObject.CustomerID,   
                        OrderObject.Freight);   
      
                    Thread.Sleep(1000);   
      
                    var OrderCustomer = OrderObject.Customer;   
                    Console.ForegroundColor = ConsoleColor.Green;   
                    Console.WriteLine("Customer ID : {0}", OrderCustomer.CustomerID);   
                    Console.ForegroundColor = ConsoleColor.White;   
                    Console.WriteLine("Customer Name : {0}", OrderCustomer.ContactName);   
                    Console.WriteLine("Phone : {0}", OrderCustomer.Phone);   
      
                    Thread.Sleep(2000);   
                }  
    


    完整的演示代码可从打包的视频演示文件中找到。

  • 相关阅读:
    1276
    1235
    1170
    Grids
    1137
    1127
    1076
    🍖Mysql三种模式的指定方式介绍
    🍖存储引擎介绍
    🍖MySQL库的详细操作
  • 原文地址:https://www.cnblogs.com/GJYSK/p/1870696.html
Copyright © 2011-2022 走看看