实体类型Object不是当前上下文的模型的一部分 [英] The entity type Object is not part of the model for the current context
问题描述
我以这种方式创建一个对象:
var assembly = typeof(Cliente).Assembly;
var Tipo = assembly.GetType(Datos。+ tipoItem);
var item = Activator.CreateInstance(Tipo);
其中tipoItem是类的名称,Datos是相应的命名空间。
Cliente是同一命名空间中的一个类。
要将对象存储在数据库中,我有一个通用的方法:
public void AddItem< TItem>(TItem item)其中TItem:class
{
db.Set< TItem> )。新增项目);
db.SaveChanges();
}
在调试时,项目对象的类型是正确的。如果tipoItem是EmailCliente,那么项的类型是Datos.EmailCliente。
AddItem方法接收项作为Datos.EmailCliente对象,但抛出异常: p>
实体类型Object不是当前上下文模型的一部分
调试时,TItem是类型对象,而不是Datos.EmailCliente。这是问题。
我尝试使用(Tipo)将项目转换为Tipo,如Tipo,Convert.ChangeType )但是没有这些工作。
如何投射项目,使AddItem方法接受它。
编辑:
根据Andrei的回答,这是正常工作的代码。
MethodInfo method = typeof(ControlDatos).GetMethod(AddItem);
MethodInfo generic = method.MakeGenericMethod(new [] {Tipo});
generic.Invoke(cd,new object [] {item});
其中cd是定义AddItem方法的ControlDatos类的实例。
当调用 AddItem(item)
时,编译器从参数派生通用类型并将其代入方法调用。因为 item
是对象类型(在编译时!) - 这是编译器使用的。所以真的你正在调用 AddItem< object>(item)
,这会给你错误。
您必须通过反射调用 AddItem
:
MethodInfo方法= typeof(ItemRepository).GetMethod(AddItem,BindingFlags.Public | BindingFlags.Static);
MethodInfo generic = method.MakeGenericMethod(Datos。+ tipoItem);
generic.Invoke(null,new object [] {item});
其中 ItemRepository
code> AddItem 方法。
I'm creating an object in this way:
var assembly = typeof (Cliente).Assembly;
var Tipo = assembly.GetType("Datos." + tipoItem);
var item = Activator.CreateInstance(Tipo);
where tipoItem is the name of a class and Datos is the respective namespace.
Cliente is a class in the same namespace.
To store the object in the database I have this generic method:
public void AddItem<TItem>(TItem item) where TItem : class
{
db.Set<TItem>().Add(item);
db.SaveChanges();
}
When debugging, the type of the item object is right. If tipoItem is "EmailCliente" then the type of item is Datos.EmailCliente.
The AddItem method receives item as a Datos.EmailCliente object but an exception is thrown:
"The entity type Object is not part of the model for the current context"
When debugging, TItem is of type object instead of Datos.EmailCliente and that's the problem.
I did try to cast item to Tipo using (Tipo), as Tipo, Convert.ChangeType(item, Tipo) but none of those work.
How can I cast item so the AddItem method accepts it.
TIA
EDIT:
Based on Andrei's answer this is the code that's working.
MethodInfo method = typeof(ControlDatos).GetMethod("AddItem");
MethodInfo generic = method.MakeGenericMethod(new[] { Tipo });
generic.Invoke(cd, new object[] { item });
where cd is an instance of the class ControlDatos where the AddItem method is defined.
When you are calling AddItem(item)
, compiler is deriving generic type from the argument and substituting it into method call. Since item
is of type object (at compile time!) - this is what compiler is using. So really you are calling AddItem<object>(item)
, which gives you the error.
To resolve this you have to make a call of AddItem
via reflection:
MethodInfo method = typeof(ItemRepository).GetMethod("AddItem", BindingFlags.Public | BindingFlags.Static);
MethodInfo generic = method.MakeGenericMethod("Datos." + tipoItem);
generic.Invoke(null, new object[]{item});
where ItemRepository
is the class which defines AddItem
method.
这篇关于实体类型Object不是当前上下文的模型的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!