Prevent EF6 from generate navigation properties

entity-framework-6 navigation-properties t4

Question

I recently began using EF6 and am developing a few T4 templates to produce some code automatically (on VS2012).

The procedure by which I generate my model from the database automatically builds all the associations based on the DB ForeignKeys. Additionally, it creates a "Navigation Property" in Associations/FK for that field.

I need my entities in "Flat Version" form without navigation properties. Basically a class having table column-related features.

Is there a way to do this using "Generate Model from Database"? The associations and nav props are still being generated when I try to update the model with the "Include foreign key columns in the Model" option unchecked.

I appreciate you.

1
1
11/13/2014 10:53:42 AM

Accepted Answer

FOUND A SOLUTION

When we have foreign keys, associations are made and Nav props instead of ordinary property are used, therefore when I was reading the conceptual model, I was getting uncurated information about the table structure (on the fields withing the FK).

Use the shop model rather than the conceptual model, I've discovered, as a solution.

Making a conceptual model in the "wrong" method

MetadataLoader loader = new MetadataLoader(this);
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);

How to get the store model the "RIGHT way"

MetadataLoader loader = new MetadataLoader(this);
StoreItemCollection ItemCollection = null;
loader.TryCreateStoreItemCollection(inputFile, out ItemCollection);
0
11/13/2014 4:57:28 PM

Popular Answer

To do this, you must modify the t4 template that creates the model classes.

There are two.tt files in your project that look something like this:ModelName.Context.tt and ModelName.tt . It is the latter that creates the model classes.

Find these two lines.

this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();

about on line 50)

and

<#=codeStringGenerator.NavigationProperty(navigationProperty)#>

likely at around line 100

then cross out these lines.

Your classes will no longer have navigation properties when you save the template.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow