Get List of Entity Models in DbContext Entity Framework Core 2.1
Asked Answered
L

2

11

I'm trying to find a way to get a List of all of the entity models in my DbContext. For instance, if I have two models called Customer and Invoice defined in C# which through code-first I created EF entities and a database, how do I now query the DbContext to get a List that has Customer and Invoice in it -- i.e., all of the entities in that context? I want to be able to call a method that returns a List of all the entities -- not the data, just a list of the entities.

It seems to me this should be so easy, but either it is not easy or I am missing something -- probably the latter. ;-).

Could someone please point me in the right direction? Thanks!!

Llovera answered 14/1, 2019 at 19:20 Comment(1)
What do you mean by "a List of all the entities -- not the data"? A list of their IDs? What does the List contain if not the data?Pectoral
E
30

You can use Model property to get the associated IModel, then GetEntityTypes method to enumerate all IEntityTypes. ClrType property of IEntityType will give you the associated class type, e.g.

DbContext db = ...;
var entityTypes = db.Model.GetEntityTypes().Select(t => t.ClrType).ToList();

IEntityType has many useful properties and (extension) methods for getting information about the primary/alternate keys, foreign keys, navigations, properties etc. in case you need them.

Edirne answered 14/1, 2019 at 19:42 Comment(13)
Thanks! This was exactly what I needed! I appreciate your help.Llovera
It throws error in run time when I try to use these types to use context.Entry(Type).Reload(). Is there any way to achieve this?Thrilling
@Thrilling Not sure what you mean - context.Entry method requires entity instance (object), not type.Edirne
Thanks @IvanStoev , I recognised too. Actually I m need a generic solution for reloading database.Thrilling
@IvanStoev How can I get the same entity types using DbContext name using reflection, not instance?Myotonia
@Myotonia You can't do that. What exactly are you going to reflect? Properties of type DbSet<>? These are not required at all. See Including types in the model. That's why we need IModel instance to start with.Edirne
@IvanStoev I can get the DbSet properties with reflection but not finding way to get entity models that are being added as modelBuilder.Entity<Employee>(). Actually I need both. What can I do in this case? Actually I need all the types included in the DbContext model using reflection.Myotonia
@Myotonia What you are asking is basically how to get what the code is doing inside OnModelCreating w/o actually executing that code, which apparently is impossible.Edirne
@IvanStoev Actually I need to add all the models from one DbConext to another so that I can query, update the same database from two DbConexts simultaneously. Could you please tell me what can be the easiest way to do so? Note: Reference of First DbContext is not available in the second DbConext assembly. So I have to do it with reflection.Myotonia
@IvanStoev Can I just add an entity to the DbContext model dynamically just before executing the query when this entity is not added to DbContext model either as DbSet or as modelBuilder.Entity<TEntity>().Myotonia
Let us continue this discussion in chat.Myotonia
After getting all the Entities, I've tried to create a instance of the Entity. Is there are way to create the instance from entityTypes ?Marilumarilyn
@Marilumarilyn Not really, at least they do not expose such a method. If you are targeting regular C# classes (and not artificial dictionary "objects" or proxies) with parameterless contructors, then probably simple reflection (e.g. Activator.CreateInstance) could work.Edirne
F
-1

You can refer documetation at: https://learn.microsoft.com/en-us/ef/core/querying/related-data

For ex. If you have blogs and posts as two tables, then you can get related table as shown below. It depends on how the relations are present in the two tables.

You can also add the where clause to get only selected records.

using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Include(blog => blog.Posts)
        .ToList();
}
Forked answered 14/1, 2019 at 19:31 Comment(1)
Thanks for your response, but what I am looking for is simply a list of all of the entity models in the context. For instance, I would like to be able to type myContext.GetEntityModels() and have it return a list of all of the models in that context -- not the data or any relationship between them.Llovera

© 2022 - 2024 — McMap. All rights reserved.