0

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.

TheDizzle
  • 1,534
  • 5
  • 33
  • 76
  • `GetTableName` ← That is strongly typed, you can't access that with a string. Your only other option is to use reflection. There are plenty of [so] question/answers on how to call a generic method dynamically using reflection. – Igor Mar 31 '20 at 13:13
  • What is it you are trying to accomplish? Why do you need to do this? Maybe there is a better way to accomplish your goal without doing any of this. If not I will try to find the most popular dupe. and mark it as such. – Igor Mar 31 '20 at 13:14
  • Basically I have to track changes when entity framework saves, updates, etc. When that happens, i can only get the class of the database object. But I need the actual table name so I can access the key information. I get the class name from the changes in EF's ChangeTracker – TheDizzle Mar 31 '20 at 13:16
  • Is this useful for you? https://stackoverflow.com/q/26355486/1260204 – Igor Mar 31 '20 at 13:18
  • no, unfortunately I'm at the mercy of another program that requires certain data. The hangup here is that I have to get the primary keys from the table and the only way I know how to do that is by using sql to look them up. and to do that I need the table name, not the class. – TheDizzle Mar 31 '20 at 13:20
  • Are you mapping your entities in EF using the Fluent API or attributes? If you are using attributes it probably makes this simpler as you can use reflection to get the key attributes from the type. – Igor Mar 31 '20 at 13:22
  • the context was scaffolded from the database. I don't see any attributes like [Key] or anything, but I also don't see anywhere that it specifies what is the keys – TheDizzle Mar 31 '20 at 13:26
  • Based on your feedback it does seem that you need to call this method dynamically as the best solution. I have marked the question as a duplicate, the duplicate explains exactly how to accomplish this. Please let me know if this does not help or answer your original question. – Igor Mar 31 '20 at 13:33

0 Answers0