DbSet.Cast() Error: Cannot create a DbSet from a non-generic DbSet for objects of type 'Entity'

后端 未结 4 758
死守一世寂寞
死守一世寂寞 2021-01-02 15:22

Version Info:

I am using C# 4.5, Entity Framework 6.0, and MEF.

Code and Unit Test

I created a Test Project to expl

4条回答
  •  情书的邮戳
    2021-01-02 16:16

    For this, I would actually suggest using reflection. In the constructor of your DbContext, you can set a property to the function pointer:

    method = this.GetType().GetMethod("Set", new Type[0]).MakeGenericMethod(typeof(UserImplementation));
    

    You can then invoke this using:

    method.Invoke(this, new object[0]);
    

    And this should return an object of type DbSet which the .Cast<>() method can then be invoked on.

提交回复
热议问题