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?)
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
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.
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.
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>();
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
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.
Member
1 Points
5 Posts
Accessing the base class properties
Mar 12, 2012 06:56 PM|MHird|LINK
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
All-Star
17916 Points
5681 Posts
MVP
Re: Accessing the base class properties
Mar 12, 2012 08:07 PM|sjnaughton|LINK
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?)
Always seeking an elegant solution.
Member
1 Points
5 Posts
Re: Accessing the base class properties
Mar 13, 2012 06:21 AM|MHird|LINK
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
All-Star
17916 Points
5681 Posts
MVP
Re: Accessing the base class properties
Mar 13, 2012 06:59 AM|sjnaughton|LINK
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?
Always seeking an elegant solution.
Member
1 Points
5 Posts
Re: Accessing the base class properties
Mar 13, 2012 07:06 AM|MHird|LINK
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
All-Star
17916 Points
5681 Posts
MVP
Re: Accessing the base class properties
Mar 13, 2012 09:00 AM|sjnaughton|LINK
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:
if the entitiy is wrapped just do this
var a = e.Entity.GetEntityAs<Comapny>();
Always seeking an elegant solution.
Member
1 Points
5 Posts
Re: Accessing the base class properties
Mar 13, 2012 09:31 AM|MHird|LINK
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.
All-Star
17916 Points
5681 Posts
MVP
Re: Accessing the base class properties
Mar 13, 2012 10:01 AM|sjnaughton|LINK
OK in that case all you need to do is cast the entity to the base entity to get at the common properties.
Always seeking an elegant solution.
Member
1 Points
5 Posts
Re: Accessing the base class properties
Mar 13, 2012 11:29 AM|MHird|LINK
Wow thank you!! That's worked, it feels like it was so obvious, thank you very very much!
All-Star
17916 Points
5681 Posts
MVP
Re: Accessing the base class properties
Mar 13, 2012 11:48 AM|sjnaughton|LINK
Sometime we can't see the wood for the trees it's no problem we have all been there :)
Always seeking an elegant solution.