实体类型 [英] The entity type

查看:79
本文介绍了实体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有词典


 公共类RP

    {


  public Dictionary< int, object > PDictionary {get;组; }


}


其中  对象一些我的


然后我有了metod  DeleteEntity


当我尝试在此metod中插入我的对象时


  var entity = rp.PDictionary [1]; //类System.Object   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; DBCommands.DeleteEntity(entity);



   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;&NBSP;

  static public void DeleteEntity< TEntity>(TEntity entity)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; TEntity:class /
$
  &NBSP; &NBSP; &NBSP; {&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;使用(var context = new ApplicationDataContext())

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; context.Set< TEntity>()附加(实体)。


这里我有一个例子附加信息:实体类型Object不是当前上下文模型的一部分。我如何解释(此对象中的getType)


}}

解决方案

您好Ahlompys,


>> ; 这里我有exeption附加信息:实体类型Object不是当前上下文模型的一部分。我如何解释(此类型中的getType)


根据您的描述和相关代码,我创建了一个演示并在我身边重现您的问题,因为您定义了一个泛型参数上的
约束与方法DeleteEntity上的类 的TEntity。你需要将class 参数传递给名为  DeleteEntity而不是object的方法。请修改你的代码如下:


#RP Class

 public class RP< T>其中T:class 
{
public Dictionary< int,T> PDictionary {get;组; }
}

#Usage:

 using(var db = new ApplicationDataContext())
{
var user = db.Users.Where(t => t.UserName ==" Hart")。FirstOrDefault();

user.FirstName =" Test" ;;

RP< User> rp = new RP< User>();

字典< int,用户> dic = new Dictionary< int,User>();
dic [1] = user;

rp.PDictionary = dic;

var entity = rp.PDictionary [1];

DBCommands.DeleteEntity(entity);

}

祝你好运,


Cole Wu


I have Dictionary

 public class RP
    {

 public Dictionary<int, object> PDictionary { get; set; }

}

where object some My classes

then I have  metod DeleteEntity

And when I try insert my object in this metod

 var entity = rp.PDictionary[1]; //class System.Object              
                DBCommands.DeleteEntity(entity);

                       

 static public void DeleteEntity<TEntity>(TEntity entity)
            where TEntity : class/
        {            
            using (var context = new ApplicationDataContext())
            {
                
                    context.Set<TEntity>().Attach(entity);

Here I have exeption Additional information: The entity type Object is not part of the model for the current context. How I can explane (getType in this object)

}}

解决方案

Hi Ahlompys,

>>Here I have exeption Additional information: The entity type Object is not part of the model for the current context. How I can explane (getType in this object)

Based on your description and related code, I create a demo and reproduce your issue on my side, Since your define a constraint on the generic parameter TEntity with class on method DeleteEntity. you need to pass class argument to method named DeleteEntity instead of object. please modify your code like this:

#RP Class

public class RP<T> where T : class
{
     public Dictionary<int, T> PDictionary { get; set; }
 }

#Usage:

using (var db = new ApplicationDataContext())
            {
                var user = db.Users.Where(t=>t.UserName == "Hart").FirstOrDefault();

                user.FirstName = "Test";

                RP<User> rp = new RP<User>();

                Dictionary<int, User> dic = new Dictionary<int, User>();
                dic[1] = user;

                rp.PDictionary = dic;

                var entity = rp.PDictionary[1]; 

                DBCommands.DeleteEntity(entity);

                            }

Best regards,

Cole Wu


这篇关于实体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆