Print Page | Close Window

Obtaining Navigation Properties by Reflection?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2904
Printed Date: 13-May-2026 at 10:41pm


Topic: Obtaining Navigation Properties by Reflection?
Posted By: jradxl
Subject: Obtaining Navigation Properties by Reflection?
Date 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




Replies:
Posted By: robertg
Date 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


Posted By: jradxl
Date 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



Print Page | Close Window