none
Add dynamic objects to EF and querying them. RRS feed

  • Question

  • I create a dynamic object using typebuilder in a runtime compiled assembly.

    I save the assembly and load it from disk.

    Then I add this assembly to a dictionary and give that to a custom DBContext that uses the assembly to create the Entitytpes.

    But now I would like to add/query entities of this dynamic type.

    Everything works (the database table is nicely created) except the dbSet.Add(), then it tells me that I cannot add test.foo because it expects a test.foo. In reflector I see that somewhere inside the test obs is TEntity fails.

    Is what I am trying to do at all possible?

    Thanks

     Ben

    //Create the assemblyDynamicEFactory factory =newDynamicEFactory("test");             //Define the entity classDictionary<StringType> entity1 =newDictionary<stringType>();             entity1.Add("Id"typeof(String));             entity1.Add("Property1"typeof(Decimal));             entity1.Add("Property2"typeof(Double));             entity1.Add("Property3"typeof(int));             Type newtype = factory.CreateDynamicType<DynamicEFEntity>("foo", entity1);             if (File.Exists(@"D:\Development\downloads\WindowsFormsApplication\bin\Debug\test.dll"))             {                 File.Delete(@"D:\Development\downloads\WindowsFormsApplication\bin\Debug\test.dll");             }             factory.SaveAssembly();             Dictionary<StringAssembly> assemblies =new Dictionary<stringAssembly>();             Assembly assembly = Assembly.LoadFile(@"D:\Development\downloads\EntityMapper\WindowsFormsApplication1\bin\Debug\test.dll");             assemblies.Add("test", assembly);             var context =new DynamicDbContext(assemblies);

                context.Database.Initialize(true); //If set to true the initializer is run even if it has already been run.       DbSet dbSet = context.Set(newtype);

    	  DbSet dbSet = context.Set(newtype);
    

                var obj = assembly.CreateInstance(dbSet.ElementType.ToString());             dbSet.Add(obj);

                                                                

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        foreach (Assembly asbly in _assemblies.Values)
        {
            foreach (Type type in asbly.GetExportedTypes())
            {
                if (type.IsClass)
                {
                    MethodInfo method = modelBuilder.GetType().GetMethod("Entity");
                    method = method.MakeGenericMethod(new Type[] { type });
                    method.Invoke(modelBuilder, null);
                }
            }
        }
        base.OnModelCreating(modelBuilder);
    }


    Sunday, June 12, 2016 7:04 PM

All replies

  • Hi Ben Geerdes,

    Could you please provide complete code and error message, which we could reproduce your issue on our side. we will try to find a solution to resolve it.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 13, 2016 1:49 PM
    Moderator