【发布时间】:2015-08-15 04:06:50
【问题描述】:
我正在尝试编写一些逻辑来根据动态表值从数据库中获取实体值。关注这篇文章让我明白了TEntity Entity framework - get entity by name
我没有遵循这里所说的内容。以下代码:
public static void PublishTable(string user, ContentFields tableToPublish)
{
var jsonData = DataAccess.GetEnvironmentJson((int)Environments.Production);
var model = JsonConvert.DeserializeObject<Models.FullDataSetModel>(jsonData);
using (var db = new LoginPageContentEntities())
{
var deployEntry = db.Deployment.Find((int)Environments.Production);
deployEntry.DateUpdated = DateTime.Now;
var pendingChangesFlag = deployEntry.GetType().GetProperty(tableToPublish.ToString());
pendingChangesFlag.SetValue(deployEntry, false);
var publishToModel = model.GetType().GetProperty(tableToPublish.ToString());
var tableValue = (IEnumerable<????>)typeof(LoginPageContentEntities).GetProperty(tableToPublish.ToString()).GetValue(db, null));
publishToModel.SetValue(model, tableValue.ToList());
var json = JsonConvert.SerializeObject(model);
deployEntry.JsonCache = json;
db.SaveChanges();
}
}
例如,如果我在IEnumerable<ProductInformation> 中传递ProductInformation 的实体,这在我更改该特定实体时有效。
运行到演员表的部分,我不遵循从上下文中获取值所需的操作。他们正在定义TEntity,但它的定义中没有任何内容。
【问题讨论】:
-
TEntity 是generic type parameter - Generics。您可以在您的方法名称
void PublishTable<TEntity>(string user...)之后定义它,然后在调用该方法时给出特定类型PublishTable<String>("username", ...);。在 MSDN An introduction to C# Generics 上可以找到一个不错的教程。 -
我查看了该教程,但没有看到任何涵盖使用泛型查找实体类型的语法的内容,但我会坚持下去。不确定如何用我的方法定义泛型。
标签: c# entity-framework reflection