使用CSharpCodeProvider生成DbContext

时间:2016-06-13 09:57:18

标签: entity-framework reflection dynamically-generated csharpcodeprovider

使用以下代码我在运行时生成OnlineShopContext

           string code = @"
           using System.Data.Entity;
           using GenerateContext.Models;
           using System.Data.Entity.Core.EntityClient;
           using GenerateContext.Models.Mapping;

           namespace GenerateContext.Models
           {
public partial class OnlineShopContext : DbContext
{
    static OnlineShopContext()
    {
        Database.SetInitializer<OnlineShopContext>(null);
    }

    public OnlineShopContext()
        : base(""Name = OnlineShopContext"")
    {
        }

  public DbSet<Customer> Customers { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderItem> OrderItems { get; set; }
    public DbSet<Product> Products { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new CustomerMap());
        modelBuilder.Configurations.Add(new OrderMap());
        modelBuilder.Configurations.Add(new OrderItemMap());
        modelBuilder.Configurations.Add(new ProductMap());
    }

}
}
";

        CSharpCodeProvider provider = new CSharpCodeProvider();
        CompilerParameters parameters = new CompilerParameters();

        // Reference to System.Drawing library
        parameters.ReferencedAssemblies.Add("System.Drawing.dll");
        parameters.ReferencedAssemblies.Add("System.Data.dll");
        parameters.ReferencedAssemblies.Add("System.Data.Entity.dll");
        parameters.ReferencedAssemblies.Add("System.ComponentModel.dll");
      parameters.ReferencedAssemblies.Add(typeof(Customer).Assembly.Location);
                       parameters.ReferencedAssemblies.Add(typeof(CustomerMap).Assembly.Location);
                       parameters.ReferencedAssemblies.Add(typeof(DbSet).Assembly.Location);
                       parameters.ReferencedAssemblies.Add(typeof(DbContext).Assembly.Location);
                       parameters.ReferencedAssemblies.Add(typeof(IQueryable).Assembly.Location);
                                      parameters.ReferencedAssemblies.Add(typeof(IQueryable<>).Assembly.Location);
                       parameters.ReferencedAssemblies.Add(typeof(System.ComponentModel.IListSource).Assembly.Location);

        parameters.GenerateExecutable = false;
        parameters.GenerateInMemory = false;
        parameters.OutputAssembly = "OutputAssembly.dll";

        CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);

        if (results.Errors.HasErrors)
        {
            StringBuilder sb = new StringBuilder();

            foreach (CompilerError error in results.Errors)
            {
                sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
            }

            throw new InvalidOperationException(sb.ToString());
        }

并且生成的程序集是:(使用ILSpy)

using GenerateContext.Models.Mapping;
using System;
using System.Data.Entity;

namespace GenerateContext.Models
{
public class OnlineShopContext : DbContext
{
    public DbSet<Customer> Customers
    {
        get;
        set;
    }

    public DbSet<Order> Orders
    {
        get;
        set;
    }

    public DbSet<OrderItem> OrderItems
    {
        get;
        set;
    }

    public DbSet<Product> Products
    {
        get;
        set;
    }

    static OnlineShopContext()
    {
        Database.SetInitializer<OnlineShopContext>(null);
    }

    public OnlineShopContext() : base("Name = OnlineShopContext")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add<Customer>(new CustomerMap());
        modelBuilder.Configurations.Add<Order>(new OrderMap());
        modelBuilder.Configurations.Add<OrderItem>(new OrderItemMap());
        modelBuilder.Configurations.Add<Product>(new ProductMap());
    }
}

}

当我想创建实例并运行查询例如FirstOrDeafult或Where时,这些方法不存在。但是由于奇怪的不匹配数据类型,有一种不能运行的Find方法

        Assembly assembly = results.CompiledAssembly;
        Type type = assembly.GetType("GenerateContext.Models.OnlineShopContext");
        dynamic db = (DbContext)Activator.CreateInstance(type);
        var obj = db.Customers.GetType();
        MethodInfo main = obj.GetMethod("Find");
        object[] parametersArray = new object[] { 100 };
        var customer = main.Invoke(obj, parametersArray);

我该怎么办? 如何将实例强制转换为类型并使用强类型来获取Where或FirstOrDefault

的数据

2 个答案:

答案 0 :(得分:0)

如果我将db.Customers转换为System.Data.Entity.DbSet<Customer>它就可以了。如下所示

        var dllversionAssm = Assembly.LoadFile(@debugPath+"OutputAssembly.dll");
        Type type = dllversionAssm.GetType("GenerateContext.Models.OnlineShopContext");
        dynamic db = (DbContext)Activator.CreateInstance(type);
        var customerService  = ((System.Data.Entity.DbSet<Customer>)db.Customers);
        var customer = customerService.FirstOrDefault(q => q.Id == 100);

现在,我如何将任何动态类型转换为其实体Type,例如db.Orders转换为System.Data.Entity.DbSet<Order>或db.Products转换为System.Data.Entity.DbSet<Product>。我的意思是我怎么能有一个通用的方法呢?

答案 1 :(得分:0)

最后我做到了。

    public System.Data.Entity.DbSet<T> Get<T>() where T : class
    {
        var dllversionAssm = Assembly.LoadFile(debugPath+"OutputAssembly.dll");
        Type type = dllversionAssm.GetType("GenerateContext.Models.OnlineShopContext");
        DbContext db = (DbContext)Activator.CreateInstance(type);
        var set = db.Set<T>();
        return set;
    }