Prevent EF6 from generate navigation properties

entity-framework-6 navigation-properties t4

Question

I've recently started to use EF6 and i'm building a couple of T4 templates to generate some code automatically (on VS2012).

I'm generating my model from the database and this process creates all the associations automatically based on the DB ForeignKeys. And generates too a "Navigation Property" for that field in Associations/FK.

I want to get a "Flat Version" of my entities without navigation properties. Just a class with properties corresponding to table columns.

Is there any way to "Generate Model from Database" and get this? i've tried to update model with the option "Include foreign key columns in the Model" unchecked, but the associations and nav props are still being generated.

thanks in advance

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

Accepted Answer

SOLUTION FOUND

As i was reading the conceptual model, i was getting innaccurated information about the table structure, because the conceptual model on the edmx, because when we have foreign keys, associations are created and Nav props instead of regular property (on the fields withing the FK).

The solution i Found is use the store model instead of Conceptual model

Getting the Conceptual Model "Wrong way"

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

Getting the Store Model "GOOD way"

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

Popular Answer

You must edit the t4 template that builds the model classes to get this done.

In your project you'll find two .tt files, something like ModelName.Context.tt and ModelName.tt. The latter is the one that builds the model classes.

Look for these two lines

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

(probably around line 50)

and

<#=codeStringGenerator.NavigationProperty(navigationProperty)#>

(probably around line 100)

and erase these lines.

Now when you save the template, your classes will be re-generated without navigation properties.



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