0

I am trying to write some logic to get entity values from the database based on a dynamic table value. Following this post has gotten me to the point of TEntity Entity framework - get entity by name

I not following what is being said here. The following code:

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();
            }
        }

For example if I pass the entity of ProductInformation in IEnumerable<ProductInformation> this works when I alter that specific entity.

Runs up to the section of the cast, I do not follow what needs to be done to get the values from the context. They are defining TEntity but there is nothing in the definition of it.

Community
  • 1
  • 1
Zach M.
  • 1,188
  • 7
  • 22
  • 46
  • TEntity is a [generic type parameter - Generics](https://msdn.microsoft.com/en-us/library/512aeb7t.aspx). You can define it after your method name `void PublishTable(string user...)` and then when calling the method give the specific type `PublishTable("username", ...);`. A nice tutorial can be found on MSDN [An introduction to C# Generics](https://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx). – keenthinker Aug 14 '15 at 18:36
  • I looked at that tutorial but I didn't see anything covering the syntax to look up an entity type with generics but I will keep at it. Not sure how defining the generic with my method would work. – Zach M. Aug 14 '15 at 18:42

2 Answers2

1

Your question seems to be - What is TEntity?

TEntity is generic. From the link provided in the comment https://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx#csharp_generics_topic2

See the code block

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

T is specifying generic. You can plugin class - like the one you have ProductInformation while instantiating Stack. For example -

var stk = new Stack<ProductInformation>();
lazy
  • 531
  • 2
  • 4
0

I managed to solve my issue with some help from the other answers, however i'm posting my solution as it applied to working with 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();
            }
        }
Zach M.
  • 1,188
  • 7
  • 22
  • 46