Say we have
using (var lobo = new MyGymEntities())
{
}
I would like to get the DbContext
name (in this case is MyGymEntities
) with reflection, is this possible?
I would like to store the string value into a variable..
Edit:
I do not want to type MyGymEntities.GetType().Name/FullName
to do this.
I'm asking if it is possible to somehow obtain this information from the assembly such as
Assembly.GetExecutingAssembly().GetName().Name; //Return Projectname
UPDATE
The following solved my problem, in case anyone
var a = Assembly.GetExecutingAssembly()
.DefinedTypes
.Where(x => x.BaseType?.Name == "DbContext").ToList();
Console.WriteLine(a.First().FullName);
Ok, guys, I'll explain why this question is not a duplicate and why I want to get the name of the DbContext.
I'm well aware that the name property of an object can be accessed by calling the GetType().Name on it, but this method can only be called by an object itself meaning I need to specify which objects name attribute I would like to retrieve.
Ex:
MyObject.GetType().Name;
However what I want to do is: to get the DbContextName(if there's any) that is on the project, solution.
why do I need this? because I have generic Forms that handle my queries
inside my Custom Forms currently, I have something like this
MyGymEntities context = new MyGymEntities();
now If I copy and paste the current solution folder to create a new project I'll have to rename the DbContext shown above to the name of the new DbContext for my solution.
however, If I use this to get the ContextName, than I'll create a new instance of the DbContext of this name and use it on my Custom Forms, therefore will need less configuration for each project I'll be creating...
var ContextName = Assembly.GetExecutingAssembly()
.DefinedTypes
.Where(x => x.BaseType?.Name == "DbContext").ToList().First().Name;
Thanks!