博客
关于我
光脚丫学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/

你可能感兴趣的文章
php -树-二叉树的实现
查看>>
PHP -算法-二路归并
查看>>
php 2条不一样 的json数据 怎么放在一个json里面_如果你是PHP开发者,请务必了解一下Composer...
查看>>
php 360 不记住密码,JavaScript_多种方法实现360浏览器下禁止自动填写用户名密码,目前开发一个项目遇到一个很 - phpStudy...
查看>>
regExp的match、exec、test区别
查看>>
php 404 自定义,APACHE 自定义404错误页面设置方法
查看>>
PHP 5.3.0以上推荐使用mysqlnd驱动
查看>>
php 7.2 安装 mcrypt 扩展: mcrypt 扩展从 php 7.1.0 开始废弃;自 php 7.2.0 起,会移到 pecl...
查看>>
php aes sha1解密,PHP AES加密/解密
查看>>
php array 分片,PHP常用数组函数小结
查看>>
php CI框架单个file表单多文件上传例子
查看>>
php composer
查看>>
reflow和repaint引发的性能问题
查看>>
Reflection反射机制原理、使用场景 及 缺陷
查看>>
php csv 导出
查看>>
php curl 实例+详解
查看>>
php curl_init函数用法(http://blog.sina.com.cn/s/blog_640738130100tsig.html)
查看>>
php curl_multi批量发送http请求
查看>>
php curl请求微信发红包接口出现错误:Peer's Certificate issuer is not recognized.
查看>>
PHP curl请求错误汇总和解决方案
查看>>