vendredi 14 août 2015

Working with reflection and entities

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.





Aucun commentaire:

Enregistrer un commentaire