I have an application that uses a plugin style methodology (Plug-in architecture for ASP.NET MVC), to populate my EF code first database. During the PreApplicationStartMethod I am pulling a list of objects from each plugin (dll) to load into entity framework. I'm trying to use this generic approach (Generic method to insert record into DB) but it says that T is an object rather than the specific type that I have specified in the plugins. As a result the insert fails because I don't have a object dbset in my context. The same happens if I manually convert the objects to their original type.
IModule.cs (main project) public interface IModule { List> Entities { get; } }
Module.cs (plugin)
public class Module : IModule
{
public List<List<object>> Entities
{
get
{
List<List<object>> entities = new List<List<object>>();
List<object> renderings = new List<object>();
renderings.Add(new FieldRendering(1, "Text", "String"));
renderings.Add(new FieldRendering(2, "Date", "Date"));
renderings.Add(new FieldRendering(3, "Hidden", "Auto"));
entities.Add(renderings);
return entities;
}
}
}
PreApplicationInit (main project)
[assembly: System.Web.PreApplicationStartMethod(typeof(Core.Modules.PreApplicationInit), "InitializeModules")]
public class PreApplicationInit
{
private static Context context;
public static void InitializeModules()
{
// code to pull in the modules (works, so suppressing for brevity)
context = new Context();
foreach (List<object> section in module.Entities)
{
foreach (object entity in section)
{
// Inspecting the entity object here indicates the correct type (FieldRendering)
Add(entity);
}
context.SaveChanges();
}
}
private static void Add<T>(T entity) where T : class
{
// Inspecting T here indicates the incorrect type (object)
// Code fails here
context.Set<T>().Add(entity);
}
}