I am trying to get the table name from the dbset property. When a change is made I get the name of the class
Type myType = entry.Entity.GetType().BaseType;
string name = myType.Name;
This gives me a string with the name of the class. In this example Employee
.
public virtual DbSet<Employee> Employees { get; set; }
I'm trying to get the name of the table by sending the class to the following method.
public static string GetTableName<T>(this DbContext context) where T : class
{
ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
return objectContext.GetTableName<T>();
}
public static string GetTableName<T>(this ObjectContext context) where T : class
{
string sql = context.CreateObjectSet<T>().ToTraceString();
Regex regex = new Regex("FROM (?<table>.*) AS");
Match match = regex.Match(sql);
string table = match.Groups["table"].Value;
return table;
}
The problem is, these methods expect a class name and not a string. How can I convert the string to a class name, or modify the method to accept the string.