博客
关于我
光脚丫学LINQ(032):探究AssociationAttribute.Storage
阅读量:574 次
发布时间:2019-03-08

本文共 3277 字,大约阅读时间需要 10 分钟。

Routing Attribute in Entity Framework

在Entity Framework中,AssociationAttribute.Storage属性 是用于定义实体类属性的存储位置,是实现对象关系映射的关键配置。以下是关于该属性的详细说明。

关于AssociationAttribute.Storage的作用

Storage 属性用于指定一个私有的属性来存储实体关系映射的集合。在Entity Framework中,集合 mapper 会根据映射信息将相关实体连接。以下是该属性的重要特性:

  • 不可选:必须设置,否则会抛出“未将对象引用设置到对象实例”的错误。
  • 依赖私有字段:公有的字段无法使用,必须是私有字段。
  • 区分大小写:属性值区分大小写,包括像VB这样的不区分大小写的语言。
  • 存在性检查:所设置的字段必须是有效的私有字段,否则即使编译通过也会在运行时出错。

关于映射字段的注意事项

在使用 Storage 属性之外,传统的方法还涉及字段映射(即不使用 Storage 属性)。这种方法的区别在于:

  • 不需要设置 Storage 属性。
  • 需要通过属性名或字段名进行映射,通常需要确保字段和属性同名且格式一致。

演示代码解析

以下是创建映射的两个实体类代码:

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
_Orders; [Association(Storage = "_Orders", ThisKey = "CustomerID", OtherKey = "CustomerID")] public EntitySet
Orders { get { return this._Orders; } set { this._Orders.Assign(value); } } } [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; [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
Customers { get { return this.GetTable
(); } } public Table
Orders { get { return this.GetTable
(); } } public NorthwindDataContext(string ConnectionString) : base(ConnectionString) { } }}

开发示例代码如下:

// 获取客户的所有订单记录string DatabaseFileName = @"C:/LINQ/Northwind.mdf";NorthwindDataContext db = new NorthwindDataContext(DatabaseFileName);Console.WriteLine(db.Customers.Select(c => c.ContactName).ToString());foreach (var CustomerObject in db.Customers) {    var CustomerOrders = CustomerObject.Orders;    foreach (var OrderObject in CustomerOrders) {        Console.WriteLine("OrderID={0}, OrderDate={1}, Freight={2}",             OrderObject.OrderID,             OrderObject.OrderDate,             OrderObject.Freight);    }}// 通过订单对象获取客户信息foreach (var OrderObject in db.Orders) {    var OrderCustomer = OrderObject.Customer;    Console.WriteLine("CustomerID={0}, Name={1}, Phone={2}",         OrderCustomer.CustomerID,         OrderCustomer.ContactName,         OrderCustomer.Phone);}

这种配置方式确保了对象关系映射的准确性,使开发者能够方便地通过类属性访问相关实体。

转载地址:http://uitiz.baihongyu.com/

你可能感兴趣的文章
python--基本代码规范
查看>>
Python--抽象类接口类
查看>>
PYTHON--欧拉习题二
查看>>
python--高级应用总结
查看>>
python-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
查看>>
Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
查看>>
Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
查看>>
Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
查看>>
python出现 unknown encoding: idna 的处理方法
查看>>
Python出现 RuntimeError: dictionary changed size during iteration 的解决方法
查看>>
Python-numpy(2)数组的操作
查看>>
Python-Numpy的tile函数用法
查看>>
python-open函数
查看>>
Python-Pandas-将特定函数应用于给定级别-多索引数据框架
查看>>
python-queue队列通信
查看>>
python-ros No module named PyKDL
查看>>
python-social-auth 是否需要会话
查看>>
Python再获年度编程语言,微软或成最大赢家
查看>>
python-week5
查看>>
Python写表情包搜索工具(非常详细)零基础入门到精通,收藏这一篇就够了
查看>>