光脚丫学LINQ(032):探究AssociationAttribute.Storage
时间:2010-11-23 来源:光脚丫思考
演示重点
此演示用来专门探究AssociationAttribute.Storage属性,包含了如下的内容:
如果不设置此属性值将会引发【未将对象引用设置到对象实例】的异常信息。不过,这只是针对实体类的属性而言,如果是使用字段来建立映射关系的话,那么可以不设置此属性值。
介绍此属性的作用。
此属性值是区分大小写的,即使是像VB这样不区分大小写的编程语言也是如此。
设置的属性值应该是一个私有字段,公有字段会发生异常。
并且所设置的必须是一个存在的私有字段,否则就算编译通过了,获取数据的时候也会出错。
演示代码
下面是建立映射的两个实体类的代码。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq.Mapping; using System.Data.Linq; namespace Demo06Ex03 { [Table(Name = "Customers")] public class Customer { [Column(Name = "CustomerID", IsPrimaryKey = true)] public string CustomerID; [Column(Name = "ContactName")] public string ContactName; [Column(Name = "Phone")] public string Phone; private EntitySet<Order> _Orders; //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 Demo06Ex03 { [Table(Name = "Orders")] public class Order { [Column(Name = "OrderID", IsPrimaryKey = true)] public int OrderID; [Column(Name = "CustomerID")] public string CustomerID; [Column(Name = "OrderDate")] public DateTime OrderDate; [Column(Name = "Freight")] public decimal Freight; private EntityRef<Customer> _Customer; [Association(Storage = "_Customer", ThisKey = "CustomerID", OtherKey = "CustomerID")] public Customer Customer { get { return this._Customer.Entity; } set { this._Customer.Entity = value; } } } }
下面是创建的自定义数据上下文的代码。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq; namespace Demo06Ex03 { public class NorthwindDataContext : DataContext { public Table<Customer> Customers { get { return this.GetTable<Customer>(); } } public Table<Order> Orders { get { return this.GetTable<Order>(); } } public NorthwindDataContext(string ConnectionString) : base(ConnectionString) { } } }
下面是通过客户对象获取此客户的所有订单对象的测试代码。
// ************************************************* // 通过客户对象获取客户的所有订单记录。 // ************************************************* string DatabaseFileName = @"C:\LINQ\Northwind.mdf"; NorthwindDataContext db = new NorthwindDataContext(DatabaseFileName); db.Log = Console.Out; var AllCustomers = from CustomerObject in db.Customers select CustomerObject; foreach (var CustomerObject in AllCustomers) { Console.WriteLine("---------------------"); Console.WriteLine("Customer ID : {0}", CustomerObject.CustomerID); Console.WriteLine("Customer Name : {0}", CustomerObject.ContactName); Console.WriteLine("Phone : {0}", CustomerObject.Phone); Thread.Sleep(1000); var CustomerOrders = CustomerObject.Orders; foreach (var OrderObject in CustomerOrders) { Console.WriteLine("OrderID={0}, OrderDate={1}, Freight={2}", OrderObject.OrderID, OrderObject.OrderDate, 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}, OrderDate={1}, Freight={2}", OrderObject.OrderID, OrderObject.OrderDate, OrderObject.Freight); Thread.Sleep(1000); var OrderCustomer = OrderObject.Customer; Console.WriteLine("Customer ID : {0}", OrderCustomer.CustomerID); Console.WriteLine("Customer Name : {0}", OrderCustomer.ContactName); Console.WriteLine("Phone : {0}", OrderCustomer.Phone); Thread.Sleep(2000); }
以上的代码已经和视频演示文件打包在一起了。
相关阅读 更多 +