代码之家  ›  专栏  ›  技术社区  ›  iLemming

如何从实体对象中获取对上级对象的引用

  •  3
  • iLemming  · 技术社区  · 14 年前

    如何从EntityObject类获取对上级objectContext的引用?

    2 回复  |  直到 14 年前
        1
  •  0
  •   Nix    14 年前

    唯一的方法就是通过一个使用关系的黑客,和一个不分离的实体。见下文。

      YourEntity someEntity = null;
    
      RelationshipManager relationshipManager = ((IEntityWithRelationships)someEntity ).RelationshipManager;
    
      IRelatedEnd relatedEnd = relationshipManager.GetAllRelatedEnds().FirstOrDefault();
    
      ObjectQuery getContext = relatedEnd.CreateSourceQuery() as ObjectQuery;
    
      YoutObjectContext c1 = (YourObjectContext)getContext .Context;
    

    祝你好运。如果您使用上面的代码,我建议使用空检查来保护它。

        2
  •  1
  •   Reza    14 年前

    请查看以下链接:

    http://blogs.msdn.com/alexj/archive/2009/06/08/tip-24-how-to-get-the-objectcontext-from-an-entity.aspx

    就像nix提到的实体对象的扩展一样。