【问题标题】:Working with reflection and entities使用反射和实体
【发布时间】: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&lt;ProductInformation&gt; 中传递ProductInformation 的实体,这在我更改该特定实体时有效。

运行到演员表的部分,我不遵循从上下文中获取值所需的操作。他们正在定义TEntity,但它的定义中没有任何内容。

【问题讨论】:

  • TEntity 是generic type parameter - Generics。您可以在您的方法名称void PublishTable&lt;TEntity&gt;(string user...) 之后定义它,然后在调用该方法时给出特定类型PublishTable&lt;String&gt;("username", ...);。在 MSDN An introduction to C# Generics 上可以找到一个不错的教程。
  • 我查看了该教程,但没有看到任何涵盖使用泛型查找实体类型的语法的内容,但我会坚持下去。不确定如何用我的方法定义泛型。

标签: c# entity-framework reflection


【解决方案1】:

您的问题似乎是 - 什么是 TEntity?

TEntity 是通用的。来自评论中提供的链接https://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx#csharp_generics_topic2

查看代码块

public class Stack<T>
{
   T[] m_Items; 
   public void Push(T item)
   {...}
   public T Pop()
   {...}
}

T 指定泛型。您可以插入类 - 就像在实例化 Stack 时拥有 ProductInformation 的类。比如——

var stk = new Stack<ProductInformation>();

【讨论】:

    【解决方案2】:

    我设法在其他答案的帮助下解决了我的问题,但是我发布了我的解决方案,因为它适用于与 EF 合作。

    public static void PublishTable<TEntity>()
            {
                var targetEntity = typeof(TEntity).Name;
                var model = JsonConvert.DeserializeObject<Models.FullDataSetModel>(DataAccess.GetEnvironmentJson((int)Environments.Production));
                using (var db = new LoginPageContentEntities())
                {
                    var deployEntry = db.Deployment.Find((int)Environments.Production);
                    deployEntry.DateUpdated = DateTime.Now;
                    deployEntry.GetType().GetProperty(targetEntity).SetValue(deployEntry, false);
                    model.GetType().GetProperty(targetEntity).SetValue(model, ((IEnumerable<TEntity>)(typeof(LoginPageContentEntities).GetProperty(targetEntity).GetValue(db, null))).ToList());
                    deployEntry.JsonCache = JsonConvert.SerializeObject(model);
                    db.SaveChanges();
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      相关资源
      最近更新 更多