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

你可能感兴趣的文章
NSRange 范围
查看>>
NSSet集合 无序的 不能重复的
查看>>
NSURLSession下载和断点续传
查看>>
NSUserdefault读书笔记
查看>>
NS图绘制工具推荐
查看>>
NT AUTHORITY\NETWORK SERVICE 权限问题
查看>>
NT symbols are incorrect, please fix symbols
查看>>
ntelliJ IDEA 报错:找不到包或者找不到符号
查看>>
NTFS文件权限管理实战
查看>>
ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
查看>>
ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
查看>>
ntp server 用法小结
查看>>
ntpdate 通过外网同步时间
查看>>
ntpdate同步配置文件调整详解
查看>>
NTPD使用/etc/ntp.conf配置时钟同步详解
查看>>
NTP及Chrony时间同步服务设置
查看>>
NTP服务器
查看>>
NTP配置
查看>>
NUC1077 Humble Numbers【数学计算+打表】
查看>>
NuGet Gallery 开源项目快速入门指南
查看>>