How to get foreign key value for independent association without hitting database?

我们两清 提交于 2019-12-22 09:28:47

问题


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;}
}

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

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)?


回答1:


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

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

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).



来源:https://stackoverflow.com/questions/12334534/how-to-get-foreign-key-value-for-independent-association-without-hitting-databas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!