使用泛型方法使用反射来转换通用实体

时间:2015-08-17 14:23:32

标签: c# entity-framework generics

我有一个名为background-color: rgba(46, 204, 113, 0.4); border: none;的方法,如下所示:

RevertChanges

我知道无论public static void RevertChanges<TEntity>() { var jsonData = DataAccess.GetEnvironmentJson((int)Environments.Production); var model = JsonConvert.DeserializeObject<Models.FullDataSetModel>(jsonData); var targetEntity = typeof(TEntity).Name; using (var db = new LoginPageContentEntities()) { db.Database.ExecuteSqlCommand(String.Format("TRUNCATE TABLE [{0}]", targetEntity)); var modelValue = model.GetType().GetProperty(targetEntity).GetValue(model, null); var table = (DbSet)(typeof(LoginPageContentEntities).GetProperty(targetEntity).GetValue(db, null)); table.AddRange((DbSet)modelValue); db.SaveChanges(); } } 是什么,都会匹配modelValue数据集。 DbSet转换不起作用,我正在努力找到如何使用泛型返回整个实体。我发现,如果我使用TEntity进行投射,我可以在桌面上调用(IEnumerable<TEntity>),但这不起作用,因为我需要能够调用添加范围。有可能:

  1. 使用泛型返回实体引用?
  2. 然后访问ToList() 保存上下文?
  3. 对于上下文,我试图替换此方法:

    AddRange

    型号:

    public static void RevertChanges(Environments targetToRestoreFrom, ContentFields tableToRevert)
            {
                var jsonData = DataAccess.GetEnvironmentJson((int)targetToRestoreFrom);
                var model = JsonConvert.DeserializeObject<Models.FullDataSetModel>(jsonData);            
                using (var db = new LoginPageContentEntities())
                {
                    switch (tableToRevert)
                    {
                        case ContentFields.ProductInformation:
                            db.Database.ExecuteSqlCommand("TRUNCATE TABLE [ProductInformation]");
                            db.ProductInformation.AddRange(model.ProductInformation);
                            break;
                        case ContentFields.DidYouKnow:
                            db.Database.ExecuteSqlCommand("TRUNCATE TABLE [DidYouKnow]");
                            db.DidYouKnow.AddRange(model.DidYouKnow);
                            break;
                        case ContentFields.MaintenanceMessage:
                            db.Database.ExecuteSqlCommand("TRUNCATE TABLE [MaintenanceMessage]");
                            db.MaintenanceMessage.AddRange(model.MaintenanceMessage);
                            break;
                        case ContentFields.TrainingEvent:
                            db.Database.ExecuteSqlCommand("TRUNCATE TABLE [TrainingEvents]");
                            db.TrainingEvents.AddRange(model.TrainingEvents);
                            break;
                        case ContentFields.VideoContent:
                            db.Database.ExecuteSqlCommand("TRUNCATE TABLE [HtmlSnippets]");
                            db.HtmlSnippets.AddRange(model.HtmlSnippets);
                            break;
                        case ContentFields.ProductMarketingUrl:
                            db.Database.ExecuteSqlCommand("TRUNCATE TABLE [ProductMarketingUrl]");
                            db.ProductMarketingUrl.AddRange(model.ProductMarketingUrl);
                            break;
                    }                
                    db.SaveChanges();
                }
            }
    

2 个答案:

答案 0 :(得分:1)

Set<T>方法允许您获得DbSet一般。

在您的情况下,您应该可以通过执行以下操作添加到集合中:

db.Set<TEntity>().AddRange(modelValue)

答案 1 :(得分:0)

您需要在Set(Type entityType)上使用DbContext method

像这样(未编译或测试):

var setType = typeof(MyModel).GetProperty(targetEntity).GetType();
db.Set(setType).AddRange(modelValue)