locked
Accessing the base class properties RRS feed

  • Question

  • User1988876724 posted

    Hi guys,

    I'm fairly new to Dynamic Data and was wondering if anyone could lend a hand.

    I'm currently working on an application where after you perform an insert on a table, you can obtain a property from that entiy's base class.

    i.e. let's say there is a property within 'EntityBase' called 'Reference' and that property is inherited by the entity I am inserting into.

    Within my PageTemplate insert.aspx.cs, I have:

      

        protected void DetailsDataSource_Inserted(object sender, EntityDataSourceChangedEventArgs e)
            {
                 var myInsertedIntoEntity = e.Entity;
            }

    This should give me access to 'myInsertedIntoEntity''s public properties and methods, but is there a way to access the properties of 'EntityBase'?

    Or am I going about this all wrong?

    Thank you in advance,

    MHird

    Monday, March 12, 2012 6:56 PM

Answers

  • User-330204900 posted

    OK in that case all you need to do is cast the entity to the base entity to get at the common properties.

    var a = e.Entity as EntityBase;
    if(a!=null)
    {
        // do you stuff here
    }
    

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 13, 2012 10:01 AM

All replies

  • User-330204900 posted

    Hi MHird, first thing first it's always best with DD to explain what you are trying to achive rather than (I am assuming you are using some version of inheritance in your model?)

    Monday, March 12, 2012 8:07 PM
  • User1988876724 posted

    Hi Stephen,

    My model uses single table inheritance, and all of my entities inherit from 'EntityBase', what occurs is whenever a new object has been inserted, EntityBase generates a GUID for that object and stores it in the databse.

    I have a partial class called Companies which inherits from EntityBase, whenever I insert a new company, EntityBase generates a new GUID for that company, and my aim is to access to the generated company guid.

    As you can see from the images in the link, in debug mode, Im able to view the properties and their values of my Company partial class in fig 1, and the properties of my base in fig 2, but I'm unsure how to access them.

     

    http://imageupload.org/en/file/199350/getting-reference.png.html

    Im interested in otbaining the Reference generated within the base which is the guid.

    Please let me know if still Im unclear and any suggestions would be really appreciated,

    Thank you

    Tuesday, March 13, 2012 6:21 AM
  • User-330204900 posted

    Your last post here seems to have gone missing I recived the notification but the post is not showing here, can you post again please?

    Tuesday, March 13, 2012 6:59 AM
  • User1988876724 posted

    Hi Stephen,

    My model uses single table inheritance, and all of my entities inherit from 'EntityBase', so whenever a new object has been inserted, EntityBase generates a GUID (which is called a Reference in the context of this project) for that object and stores it in the database.

    I have a partial class called Companies which inherits from EntityBase, whenever I insert a new company, EntityBase generates a new GUID for that company, and my aim is to gain access to the generated company Reference.

    As you can see from the link, in debug mode, Im able to view the properties and their values of my Company partial class in fig 1, and the properties of my base in fig 2, but I'm unsure how to access them.



    http://imageupload.org/en/file/199350/getting-reference.png.html



    Im interested in otbaining the Reference generated within the base which is the Reference.

    Please let me know if still Im unclear and any suggestions would be really appreciated,

    Thank you

    Tuesday, March 13, 2012 7:06 AM
  • User-330204900 posted

    That should work like ordinary class inheritance your problem come fromt he fact that entitis in EF are often wrapped in an EF wrapper you can use this extension method to get at the entity:

    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;
    }
    

    if the entitiy is wrapped just do this

    var a = e.Entity.GetEntityAs<Comapny>();

    Tuesday, March 13, 2012 9:00 AM
  • User1988876724 posted

    Thanks, although e.Entity is a runtime property, so e.Entity.GetEntityAs<Comapny>(); throws an error, because I believe e.Entity merly refers to the entity which at runtime is being inserted into.

    Tuesday, March 13, 2012 9:31 AM
  • User-330204900 posted

    OK in that case all you need to do is cast the entity to the base entity to get at the common properties.

    var a = e.Entity as EntityBase;
    if(a!=null)
    {
        // do you stuff here
    }
    

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 13, 2012 10:01 AM
  • User1988876724 posted

    Wow thank you!! That's worked, it feels like it was so obvious, thank you very very much!

    Tuesday, March 13, 2012 11:29 AM
  • User-330204900 posted

    Sometime we can't see the wood for the trees it's no problem we have all been there :)

    Tuesday, March 13, 2012 11:48 AM