我需要将SQL返回值动态转换为这样的模型
var data = _db.Query<myModel>(myStoredProcedure, p, commandType: System.Data.CommandType.StoredProcedure);
由于模型在转换中是显式的,因此上面的方法应能正常工作。
但是,myModel可以是我在项目中拥有的任何类型的模型类。我能够通过反射和使用名为“ TableName”的字符串变量来获取实际模型 我以为可以使用此例程进行设置,但是出现“ entityObject是变量但使用得像类型”的错误
//First we need to find the project that holds all of our entity models in the assembly
var assembly = AppDomain.CurrentDomain.GetAssemblies().Where(x => x.FullName.Contains("MyProject.Models")).FirstOrDefault();
//Now we need to search through the assembly to match the Entity to the supplied TableName
var type = assembly.GetTypes()
.FirstOrDefault(t => t.Name == localTableName);
//Once found then we create a dynamic instance of the entity using reflection
if (type != null)
{
var ctx = new MyProject.Models.Entities();
//Create the DBSet here
System.Data.Entity.DbSet myDbSet = ctx.Set(type);
//Now create the actual entity reference which is just an object at this point
var entityObject = myDbSet.Create();
--> Errors here var data = _db.Query<entityObject>(myStoredProcedure, p, commandType: System.Data.CommandType.StoredProcedure);
}
我应该使用ExpandoObject吗?如果是这样,如何将expandoobject转换为entityClass?