none
List ObjectSets within ObjectContext RRS feed

  • Question

  • I realize that the ObjectContext has a list of strongly typed ObjectSet<T> members for each of my tables I have defined in the EF model. What I am looking to do, is get a list of the ObjectSet objects, and retain a reference to them.

    The best I have done so far, is getting a list of EntityTypes.  Unfortunately, I don't know how to instatiate an ObjectSet from an EntityType...

    E.g. getting a list of EntityTypes from my model:

          var tables = featureData.MetadataWorkspace.GetItems(System.Data.Metadata.Edm.DataSpace.CSpace)
            .Where(t => t.BuiltInTypeKind == System.Data.Metadata.Edm.BuiltInTypeKind.EntityType)
            .ToList();
    

    Any help?

    Thursday, July 28, 2011 7:52 PM

Answers

  • Hello,

    I think you can try the following code snippets to get more detailed information about ObjectSet<object>.

    public ObjectSet<Parent> Parents
    {
       get
       {
          if ((_Parents == null))
          {
             _Parents = base.CreateObjectSet<Parent>("Parents");
          }
          return _Parents;
       }
    }
    
    using (var myEntity = new XueyunEntities())
    {
        object obj;
        myEntity.TryGetObjectByKey(GetEntityKey(myEntity.Parents, 1), out obj);
        var parents = obj as Parent;
        Console.WriteLine(parents.startDate);
    }
    
    public static EntityKey GetEntityKey<T>(ObjectSet<T> objectSet, object keyValue) where T : class
    {
        var entitySetName = objectSet.Context.DefaultContainerName + "." + objectSet.EntitySet.Name;
        var keyPropertyName = objectSet.EntitySet.ElementType.KeyMembers[0].ToString();
        var entityKey = new EntityKey(entitySetName, new[] { new EntityKeyMember(keyPropertyName, keyValue) });
    
        var objectName = objectSet.Context.GetObjectByKey(entityKey);
    
        return entityKey;
    }
    

    Hope this helps.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 1, 2011 7:53 AM

All replies

  • Followup:

     

    I simply chose to use reflection on the ObjectContext, using GetProperty(...).GetValue(...).


    e.g.

     

     

       container.GetType().GetProperties()
        .Where(p => p.GetValue(container, null) is IQueryable)
    


     


    • Edited by ajhuddymsdn Thursday, July 28, 2011 9:13 PM cleanup
    Thursday, July 28, 2011 8:37 PM
  • Hello,

    I think you can try the following code snippets to get more detailed information about ObjectSet<object>.

    public ObjectSet<Parent> Parents
    {
       get
       {
          if ((_Parents == null))
          {
             _Parents = base.CreateObjectSet<Parent>("Parents");
          }
          return _Parents;
       }
    }
    
    using (var myEntity = new XueyunEntities())
    {
        object obj;
        myEntity.TryGetObjectByKey(GetEntityKey(myEntity.Parents, 1), out obj);
        var parents = obj as Parent;
        Console.WriteLine(parents.startDate);
    }
    
    public static EntityKey GetEntityKey<T>(ObjectSet<T> objectSet, object keyValue) where T : class
    {
        var entitySetName = objectSet.Context.DefaultContainerName + "." + objectSet.EntitySet.Name;
        var keyPropertyName = objectSet.EntitySet.ElementType.KeyMembers[0].ToString();
        var entityKey = new EntityKey(entitySetName, new[] { new EntityKeyMember(keyPropertyName, keyValue) });
    
        var objectName = objectSet.Context.GetObjectByKey(entityKey);
    
        return entityKey;
    }
    

    Hope this helps.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 1, 2011 7:53 AM