views:

36

answers:

2

I need to be able to iterate of a list of EntityObjects that are in an EF model.

For Example..

foreach (System.Data.Objects.DataClasses.EntityObject eObject in ????)
{
}

From what I can see the model context has no such public enumerator.

Anyone ever done this?

+1  A: 

From your comments, I think, despite the code in your question, that you are asking for the list of entity types in the CSDL rather than a list of objects. There's a demo of that here.

Craig Stuntz
+1 great link - thanks!
marc_s
Hey Craig, thank you very much for the link. That article lead me down the right path to figuring this out. I am going to answer the question aswell to link my exact code that solved my issue because it is a bit different than the article.
Matthew McDonald
+1  A: 

The problem here was i needed a dynamic way to iterate over the EntityObjects which are also consider types in the EDMX. I needed to list the Entity name and its properties. Thanks very much to Craig Stuntz for leading me down the right path to solve this issue. Here is the final code i came up with to solve my problem.

EmployeesEntities context = new EmployeesEntities();
MetadataWorkspace workspace = context.MetadataWorkspace;

workspace.LoadFromAssembly(Assembly.Load(@"WindowsFormsApplication10"));

ItemCollection itemCol = workspace.GetItemCollection(DataSpace.OSpace);

StringBuilder sb = new StringBuilder();
foreach (EdmType eType in itemCol)
{
    if (eType.GetType().BaseType == typeof(System.Data.Metadata.Edm.EntityType))
    {
        sb.Append(string.Format("Entity: {0} ", eType.Name));
        foreach (EdmProperty prop in 
            ((System.Data.Metadata.Edm.EntityType)(eType)).Properties)
        {
            sb.Append(string.Format("Property: {0} ", prop.Name));
        }

    }
}
MessageBox.Show(sb.ToString());
Matthew McDonald