In the following code, the type domainObjectchanges (but ends with DO, which I crop next to get the corresponding table name). Having the name of the table and its type, I want to update an existing object — its name matches the name tableNamedue to EF — in the database with the new property values from domainObject. Therefore, I must first find the POCO in the table with the same IDin order to overwrite this. This is the code so far:
public void Update(object domainObject)
{
Type type = domainObject.GetType();
string tableName = type.Name.Substring(0, type.Name.Length - 2);
PropertyInfo tableProp = typeof(MyDbContext).GetProperty(tableName);
Type tableType = tableProp.PropertyType;
Type pocoType = tableType.GetGenericArguments()[0];
int id = (int)type.GetProperty("ID").GetValue(domainObject);
using (var context = new MyDbContext())
{
object table = tableProp.GetValue(context);
MethodInfo singleMethod = tableType.GetMethod("Single");
}
}
Usually, knowing the actual table, and not just its type, I now get POCO through
var poco = context.TableName.Single(item => item.ID == id);
There are 2 problems here:
(1) Single- extension method.
(2) I have no idea how to get a lambda expression in a form objectto pass it to Invokefrom Single.
- Reflection, ? (, table [ , , ], , , EF - "" , Add , ID , ). , , , !