简体   繁体   中英

SqlException: Invalid object name, with an object created at runtime

I have a dynamic object named 'dbSet' that is created at runtime, I think it contains a DbSet

dynamic dbSet = context.RegisterType(GenericPOCO);

I create the DbSet object whit the following code

internal object RegisterType<T>(T genericPOCO) where T: class
    {
        var m = this.GetType().GetMethod("Set");
        var generic = m.MakeGenericMethod(genericPOCO.GetType());

        var result = generic.Invoke(this, null);

        return result;
    }

After, I try to create a queryble object whit the next line:

var item = await EntityFrameworkQueryableExtensions.FirstOrDefaultAsync(dbSet);

In the last line I receive the next message

SQLException: Invalid object name 'TestGatito'

'TestGatito' is the type of the dynamic object created at runtime, this name can be variable so I can't register the object in the model

I could finally resolve the problem

I had to create a new context to register the new object, the new context is same than the main context so whit the nex context I can add new Entities and aply reflection to invoke all methods of DbContext like 'FirstOrDefault, Find, List, etc'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM