I want to implement a function that accepts a DbSet (non-generic), a string, and object, and returns DbSet. something like the following pseudu:
public static DbSet Any(DbSet set, string propertyName, objectParameter)
{
var tableName = set.TableName;
var columnName = set.GetColumnNameForProperty(propertyName);
var query = string.Format("SELECT TOP(1) {0} FROM {1} WHERE {0} = {2}",
columnName,
tableName,
objectParameter);
}
I think that SQL query is enough since I’ll be able to execute it directly on the Database (context.Database.ExecuteSql).
What I want to do is get the table name from the given DbSet, then the column name in the database.
It is not possible from non generic
DbSetbut this problem can be easily solved by using:Returning
DbSetdoesn’t make sense because once you query data it is notDbSetanymore.The bigger problem is getting table name from generic
DbSet/ObjectSetbecause this information is not available from those classes. It is almost impossible to get it at all because it requires accessing non public members of items fromMetadataWorkspace.