开发者

How to get a list of EntityObject's from an EF model

开发者 https://www.devze.com 2023-01-06 10:53 出处:网络
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 ????)

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?


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());


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消