locked
Why can't I get actual object from EntityDataSourceWrapper in Dynamic Data? RRS feed

  • Question

  • User745141034 posted

    code:          

    object entity;            

     ICustomTypeDescriptor entityDescriptor= FormView1.DataItem as ICustomTypeDescriptor;            

    if (entityDescriptor != null)            

     {                

    entity = entityDescriptor.GetPropertyOwner(null);            

     }            

    else

                 {

                    entity = FormView1.DataItem;

                }            

     System.Type t = entity.GetType();

    t is type of "System.Data.Entity.DynamicProxies.Client_0DC1BF3A0B83758B13396E74DD9D6EFA02973CC4A4BD048F0C2CA22FF4862377",why?

    Entity framework is 5.0   ,   .net framework is 4.5

    How to get actual object from EntityDataSourceWrapper in Dynamic Data?

     

    Tuesday, August 21, 2012 12:06 AM

Answers

  • User-330204900 posted

    Ah, I think I under stand if you look at DataItem ther it will be null, not sure why but I have had the same issue I will try and build a repro and send it to the team thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 5, 2012 10:14 AM

All replies

  • User-330204900 posted

    Hi zzdfc, try this extension method I wrote :)

    public static TEntity GetEntityAs<TEntity>(this object dataItem)
        where TEntity : class
    {
        var entity = dataItem as TEntity;
    
        if (entity != null)
            return entity;
    
        var td = dataItem as ICustomTypeDescriptor;
    
        if (td != null)
            return (TEntity)td.GetPropertyOwner(null);
    
        return null;
    }
    
    public static Object GetEntity(this object dataItem)
    {
        var td = dataItem as ICustomTypeDescriptor;
    
        if (td != null)
            return td.GetPropertyOwner(null);
    
        return null;
    }

    you us them like this

    var presetSession = Row.GetEntityAs<Models.PersonSessionRecord>();
    

    and the other like this

    var entity = e.Item.DataItem.GetEntity();
    
    Tuesday, August 21, 2012 9:38 AM
  • User745141034 posted

    Hi sjnaughton:

          I have already tried your method that can't get real object in EF5 and dynamic data 4.

    But can get real type like this:

     var entity = e.Item.DataItem.GetEntity();

    System.Type realType=ObjectContext.GetObjectType(entity);

    thanks.

    Tuesday, August 21, 2012 10:15 PM
  • User3866881 posted

    I have already tried your method that can't get real object in EF5 and dynamic data 4.

    What does this mean?

    Wednesday, August 22, 2012 10:06 PM
  • User-330204900 posted

    is this with VS2012?

    Thursday, August 23, 2012 4:07 AM
  • User745141034 posted

    This is with VS2012,.net 4.5,EF5,asp.net 4

    Thursday, August 23, 2012 4:35 AM
  • User-330204900 posted

    Hi zzdfc, I will try and have a look at that this weekend :)

    Saturday, August 25, 2012 10:47 AM
  • User745141034 posted

    ???

    Wednesday, September 12, 2012 8:28 AM
  • User745141034 posted

    Who can help me,please?

    Sunday, September 16, 2012 12:15 AM
  • User-330204900 posted

    Hi Zzdfc, I have tried my code and all works fine in VS2012 can you emil me direct so we can talk this through?

    Tuesday, October 2, 2012 4:51 AM
  • User745141034 posted

    Hi sjnaughton:

     

    object entity; 

    ICustomTypeDescriptor entityDescriptor= FormView1.DataItem as ICustomTypeDescriptor;           

    if (entityDescriptor != null)           

    {               

    entity = entityDescriptor.GetPropertyOwner(null);           

    }           

    else

                 {

                    entity = FormView1.DataItem;

                }           

    System.Type t = entity.GetType();

    t is type of "System.Data.Entity.DynamicProxies.Client_0DC1BF3A0B83758B13396E74DD9D6EFA02973CC4A4BD048F0C2CA22FF4862377",

    but t actually is "System.Data.Entity.DynamicProxies.Client"

    How to convert "System.Data.Entity.DynamicProxies.Client_0DC1BF3A0B83758B13396E74DD9D6EFA02973CC4A4BD048F0C2CA22FF4862377" to "System.Data.Entity.DynamicProxies.Client"?

    Tuesday, October 2, 2012 9:40 AM
  • User-330204900 posted

    Hi Zzdfc, where is this code being run? and what are you trying to achive?

    Tuesday, October 2, 2012 11:02 AM
  • User745141034 posted

    Hi sjnaughton:

     this code being run in edit.aspx.cs page:

    protected void FormView1_DataBound(object sender, EventArgs e)        

     {

    object entity;

    ICustomTypeDescriptor entityDescriptor= FormView1.DataItem as ICustomTypeDescriptor;          

    if (entityDescriptor != null)          

    {              

    entity = entityDescriptor.GetPropertyOwner(null);          

    }          

    else

                 {

                    entity = FormView1.DataItem;

                } 

    }

     

    How to convert entity from "System.Data.Entity.DynamicProxies.Client_0DC1BF3A0B83758B13396E74DD9D6EFA02973CC4A4BD048F0C2CA22FF4862377" to "System.Data.Entity.DynamicProxies.Client"?

    Friday, October 5, 2012 9:40 AM
  • User-330204900 posted

    Ah, I think I under stand if you look at DataItem ther it will be null, not sure why but I have had the same issue I will try and build a repro and send it to the team thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 5, 2012 10:14 AM
  • User-1150461690 posted

    This is great!  Thanks!

    Thursday, February 13, 2014 4:26 PM
  • User1837504520 posted

    Use AutoMapper 4.2.1 It is having DynamicMap which can remove the Proxy from the object.

            var parents = parentsRepo.GetAll().ToList();
            Mapper.CreateMap<Parent,ParentDto>();
            var parentsDto = Mapper.DynamicMap<List<ParentDto>>(parents);

    Thursday, July 6, 2017 1:49 PM