如何获得独立相关外键值没有击中数据库? [英] How to get foreign key value for independent association without hitting database?

查看:177
本文介绍了如何获得独立相关外键值没有击中数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的独立协会(与延迟加载)来访问相关实体在我的code第一款车型,例如

I am using independent associations (with lazy loading) to access related entities in my code first model e.g.

public class Aaa {
  public int AaaId {get;set;}
  public string SomeValue {get;set;}
}

public class Bbb {
  public int BbbId {get;set;}
  public string SomeValue {get;set;}
  public virtual Aaa MyIndependentAssociation {get;set;}
}

但我想知道如何访问的 MyIndependentAssociation 外键值没有实际加载的记录。

but I am wondering how to access the foreign key value of MyIndependentAssociation without actually loading the record.

我假设了 Aaa_AaaId 值实际加载的时候我检索 BBB 实体从数据库中(根据的在实体表调试可视化反正)。

I am assuming the Aaa_AaaId value is actually loaded when I retrieve a Bbb entity from the database (according the debug visualizer on the entity table anyway).

如何获得访问值(除增加一个外键关联到我的模型)?

How do I get access to the value (other than adding a Foreign Key association into my model)?

推荐答案

这是可能的。随着你的榜样模型,你可以找到外键值的方式如下:

It's possible. With your example model you can find the foreign key values the following way:

Bbb bbb = myDbContext.Bbbs.First();

var objectContext = ((IObjectContextAdapter)myDbContext).ObjectContext;

var relMgr = objectContext.ObjectStateManager.GetRelationshipManager(bbb);
var relEnds = relMgr.GetAllRelatedEnds();
var relEnd = relEnds.Single(); // because yor model has exactly one relationship
var entityRef = relEnd as EntityReference<Aaa>;    
var entityKey = entityRef.EntityKey;

int foreignKeyValue = (int)entityKey.EntityKeyValues[0].Value;

// to confirm that no database query happened
Console.WriteLine(entityRef.IsLoaded); // outputs false

在,你必须在 BBB 类多重关系,也许是更普遍的情况下,甚至超过一个导航属性​​指的是 AAA 你需要找到在 relEnds 枚举正确的元素。你也可以有复合的外键。它是这样的话:

In the more general case where you have multiple relationships in the Bbb class and maybe even more than one navigation property refering to Aaa you need to find the correct element in the relEnds enumeration. You could also have composite foreign keys. It would look like this then:

Bbb bbb = myDbContext.Bbbs.First();

var objectContext = ((IObjectContextAdapter)myDbContext).ObjectContext;

var relMgr = objectContext.ObjectStateManager.GetRelationshipManager(bbb);
var entityRef = relMgr.GetRelatedReference<Aaa>(
    "MyEntityNamespace.Bbb_MyIndependentAssociation",
    "Bbb_MyIndependentAssociation_Target");
var entityKey = entityRef.EntityKey;

object[] compositeForeignKeyValues =
    entityKey.EntityKeyValues.Select(e => e.Value).ToArray();

// to confirm that no database query happened
Console.WriteLine(entityRef.IsLoaded); // outputs false

请注意,即 IsLoaded 如果您检查的EntityRef 在调试对象可引起相关对象被加载(即使迟缓装载被禁用)。

Note, that IsLoaded can be true if you inspect the entityRef object in the debugger which can cause the related object to be loaded (even though lazy loading is disabled).

这篇关于如何获得独立相关外键值没有击中数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆