What I am trying to achieve is a generic way to use the add-method on a parent entity to add all the children of that entity type. This must be done to “chain” the entity objects together so entity framework can do its “magic” with the database keys (they are all identity columns).
This is the kind of code that I use today:
EntityObject ParentEntityObject;
EntityObject ChildEntityObject;
((Application)ParentEntityObject).MessageLists.Add((MessageList)ChildEntityObject);
I have “Application” and “MessageList” as strings in my program. I would therefore like to do two things:
- Use GetType with the strings so ParentEntityObject and ChildEntityObject could be dynamically casted.
- Use some kind of reflection magic to get the ‘MessageLists.Add’ function. Again; let’s assume that I have ‘MessageLists’ as a string.
I am unsure of how this could be done. Any advice is greatly appreciated!
You can try expression trees to do the “magic” stuff.
For example:
Usage: