How can I extract list of Types from ObjectContext?

isxaker

I use Entity Framework. I create Entity Model. I want to get all types of instances from DbContext in runtime.

public class MyClass
{
   WdmEntities _context =  = new WdmEntities();
   ObjectContext objContext = ((IObjectContextAdapter)_context).ObjectContext;
   EntityContainer container = objContext.MetadataWorkspace.GetEntityContainer(objContext.DefaultContainerName, DataSpace.CSpace);
   //even if c=>c.FullName
   List<string> nameTypes = container.BaseEntitySets.OfType<EntitySet>().Select(c=>c.Name).ToList();
   List<Type> types = new List<Type>();

   foreach(var name in nameTypes)
   {
    //.GetType return null
    types.Add(Type.GetType(name));
   }
}
Wouter de Kort

Try the following:

MyContext _context = new MyContext();
ObjectContext objContext = ((IObjectContextAdapter)_context).ObjectContext;
var nameTypes = objContext.MetadataWorkspace.GetItems<EntityType>(DataSpace.OSpace);
List<Type> types = new List<Type>();

foreach (var entityType in nameTypes)
{
    var type = Type.GetType(entityType.FullName + "," + Assembly.GetExecutingAssembly().FullName);
    types.Add(type);
}

By using the GetItems<EntityType> method you directly load all your entities from the ObjectContext. The parameter that you pass to this method specifies where you want to look for entities. You should use the OSpace value to request entity types from the object model. This will map to CLR types except when those types are nested. In that case, you get a composed name with all the outer types in it.

Type.GetType expects both the full name of the object and the full name of the assembly. In this example, I use Assembly.GetExecutingAssembly. If your entities are defined in another assembly you need to change this.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I extract list from list in prolog?

분류에서Dev

How can I extract sentences from any search engine?

분류에서Dev

How can I recreate a list of list of floats from the contents of a file?

분류에서Dev

How can I remove an entry from a list in a shells script?

분류에서Dev

How can I list last updated packages from terminal?

분류에서Dev

How can i have distinct selection from a list on the basis of id?

분류에서Dev

How to extract string from python list?

분류에서Dev

How to extract tuples from a list of lists in Haskell

분류에서Dev

How can i compare string and character types?

분류에서Dev

How can I extract multiple items from one page wit Portia/Scrapy Spider

분류에서Dev

Using Perl, how can I extract from a log file only the errors that happened during a specific minute?

분류에서Dev

How can I read and extract information from a DDS-1 tape today?

분류에서Dev

How can i restrict user from not selecting other file types using input type file with react and typescript?

분류에서Dev

How can I list all packages I've installed from a particular repository?

분류에서Dev

How to extract data from a list of tuples which length could be variable

분류에서Dev

How to extract distances from delaunay triangulation to list object in R

분류에서Dev

Excel: How to extract repeated values from randomly ordered list?

분류에서Dev

How can I extract all links (href) in an HTML file?

분류에서Dev

How can I extract text between parentheses containing a specific word?

분류에서Dev

How can I extract text before a character or string in a batch file?

분류에서Dev

How can I check if a line contains specific string from a list of strings?

분류에서Dev

How can i get from the List<string> of files each time another file?

분류에서Dev

How I can remove more than 1 items from drop down list?

분류에서Dev

How can I create List of rectangles in TypeScript?

분류에서Dev

How can I centralize a list with css?

분류에서Dev

How can I create a resource list on Apigility?

분류에서Dev

How can I return a list of summary statistics?

분류에서Dev

How do I extract embedded payloads from a Wix burn installer?

분류에서Dev

How to extract only the image path from the array I'm getting?

Related 관련 기사

  1. 1

    How can I extract list from list in prolog?

  2. 2

    How can I extract sentences from any search engine?

  3. 3

    How can I recreate a list of list of floats from the contents of a file?

  4. 4

    How can I remove an entry from a list in a shells script?

  5. 5

    How can I list last updated packages from terminal?

  6. 6

    How can i have distinct selection from a list on the basis of id?

  7. 7

    How to extract string from python list?

  8. 8

    How to extract tuples from a list of lists in Haskell

  9. 9

    How can i compare string and character types?

  10. 10

    How can I extract multiple items from one page wit Portia/Scrapy Spider

  11. 11

    Using Perl, how can I extract from a log file only the errors that happened during a specific minute?

  12. 12

    How can I read and extract information from a DDS-1 tape today?

  13. 13

    How can i restrict user from not selecting other file types using input type file with react and typescript?

  14. 14

    How can I list all packages I've installed from a particular repository?

  15. 15

    How to extract data from a list of tuples which length could be variable

  16. 16

    How to extract distances from delaunay triangulation to list object in R

  17. 17

    Excel: How to extract repeated values from randomly ordered list?

  18. 18

    How can I extract all links (href) in an HTML file?

  19. 19

    How can I extract text between parentheses containing a specific word?

  20. 20

    How can I extract text before a character or string in a batch file?

  21. 21

    How can I check if a line contains specific string from a list of strings?

  22. 22

    How can i get from the List<string> of files each time another file?

  23. 23

    How I can remove more than 1 items from drop down list?

  24. 24

    How can I create List of rectangles in TypeScript?

  25. 25

    How can I centralize a list with css?

  26. 26

    How can I create a resource list on Apigility?

  27. 27

    How can I return a list of summary statistics?

  28. 28

    How do I extract embedded payloads from a Wix burn installer?

  29. 29

    How to extract only the image path from the array I'm getting?

뜨겁다태그

보관