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

你可能感兴趣的文章
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>