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

你可能感兴趣的文章
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>