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

梦想与她 提交于 2020-01-05 17:38:46

问题


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:


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



回答2:


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.



来源:https://stackoverflow.com/questions/3214701/how-to-get-a-list-of-entityobjects-from-an-ef-model

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