New Posts New Posts RSS Feed: Obtaining Navigation Properties by Reflection?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Obtaining Navigation Properties by Reflection?

 Post Reply Post Reply
Author
jradxl View Drop Down
Newbie
Newbie


Joined: 14-Jun-2011
Posts: 30
Post Options Post Options   Quote jradxl Quote  Post ReplyReply Direct Link To This Post Topic: Obtaining Navigation Properties by Reflection?
    Posted: 17-Aug-2011 at 12:36pm
Hi,
Assuming the typical DF technique of putting the .edmx and the generated .cs in an Assembly, I want to find the Navigation properties by reflection over the Assembly DLL (for code generation reasons during development).

Of course I can find all the members of the NestedProperty, PropertyMetadata but the only way I can think of deciding whether there are navigation properties is to look for the part word Navigation in the FieldType.

                Type myType = typeof(MyDomainModel.MyEntity);
                var nestedtypes = myType.GetNestedType("PropertyMetadata"BindingFlags.Public);
                var navproperties = nestedtypes.GetFields().Where(f => f.FieldType.Name.Contains("Navigation"));
Is that robust?
Is there a better way?

thanks
John

Back to Top
robertg View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 15-Mar-2011
Location: California
Posts: 87
Post Options Post Options   Quote robertg Quote  Post ReplyReply Direct Link To This Post Posted: 17-Aug-2011 at 2:14pm
John,
 
It sounds like you're trying to do something very complex, which means there's always the possibility of unforeseen difficulties. Would you mind providing some more information on what you're trying to accomplish? Are you trying to override code generation?
 
In general, filtering against the "Navigation" string for  your properties should be pretty safe, since you're using it against generated code. It's certainly what I'd do.
 
If you can provide some more information on your use-case, we'll think it through a bit more to make sure it does match your need, though. It may be that there's another technique you could use which is more straight-forward than building a model of your model with reflection. 
 
Yours
Robert
Back to Top
jradxl View Drop Down
Newbie
Newbie


Joined: 14-Jun-2011
Posts: 30
Post Options Post Options   Quote jradxl Quote  Post ReplyReply Direct Link To This Post Posted: 17-Aug-2011 at 3:23pm
Hi Robert,
Thanks for your quick reply, and your considered response.

Can I offer this, which will illustrated what I was trying....

With DF 6.1.2 (and EF 4.1) we are moving to not having a .edmx file, by using Code First.

But we have a T4 code generator (for non DF things) that consumes the .edmx in an equally complex way within VS2010.

Now that VS2010 SP1 does not lock DLLs while they are being used within T4, I was obtaining the model by reflection.
It's working extremely well, and the Dev, Test, Debug cycle is very easy and quick, whether one is changing the Model, it's partial classes or the T4 template.

But until I tried I didn't realise that the Navigation info is so difficult to obtain from the model assembly by reflection.
I think the info the code generator needs will need to be added manually in a partial class in a similar form to your CustomerMetadata approach - pity, was hoping I could make it "automatic".

thanks
John
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down