none
The entity type RRS feed

  • Question

  • 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)

    }}




    • Edited by Ahlompys Thursday, May 11, 2017 3:21 PM
    Thursday, May 11, 2017 3:18 PM

All replies

  • 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


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 12, 2017 8:15 AM
    Moderator
  • Thank you cole wu But maybe You nown how I can convert object to real Class something like where User User 2 Class

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

    var (User or User2 System must detect) userClass = rp.PDictionary[1].GetClass();

    or

    var (User or User2 System must detect)userClass = rp.PDictionary[1].Clone();

    and then

     DBCommands.DeleteEntity(userClass );




    • Edited by Ahlompys Friday, May 12, 2017 11:50 AM
    Friday, May 12, 2017 11:45 AM
  • Hi Ahlompys,

    If we use generic class, we could not achieve the requirement that you provide. if you use object, you need to convert to User when you call DeleteEntity method. like this:

     var user = db.Users.Where(t=>t.UserName == "Hart").FirstOrDefault();
    user.FirstName = "Test";
    
     //RP<User> rp = new RP<User>();
    
    RP rp = new RP();
    
    Dictionary<int, object> dic = new Dictionary<int, object>();
     dic[1] = user;
    
     rp.PDictionary = dic;
    
     User entity = (User)rp.PDictionary[1]; //class System.Object              
    
    DBCommands.DeleteEntity(entity);

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 16, 2017 6:14 AM
    Moderator