Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make all entities access:internal instead of public in EDMX?

I'd like my Entity Framework model to generate entities with internal access modifier, instead of public. I use the EF model in a library and I want only a single class (some controller) to be accessible from outside.

Is there any simple way to make the EF model generation use internal modifer instead of public, including model regeneration on update?

like image 634
Kornelije Petak Avatar asked Mar 01 '12 09:03

Kornelije Petak


3 Answers

I've just gone over this myself with the latest version of Entity Framework (6.2.0). On line 314 of the .tt file I found this:

public string EntityClassOpening(EntityType entity)

On line 319 I just changed it to be a string stating internal. Ran the tool and now all my entities are internal.

like image 180
Matthew Speakman Avatar answered Oct 12 '22 13:10

Matthew Speakman


Sure, in Model Browser window select > EntityTypes

Model Browser

and on your type set in Properties window access modifier. You should also modify in EntityContainer > EntitySets the modifier for set, as if the type is internal, the set should be at least internal too (default public).

Optinally you can use T4 template, where you can directly modify the access modified being generated.

like image 29
cincura.net Avatar answered Oct 12 '22 14:10

cincura.net


It's very much like the anwer I got on a similar question. But in your case you want to customize the access modifier of the classes, rather than the ObjectContext.

Adapted after hvd's answer:

The designer has an option "Add Code Generation Item". If you use this, you'll get several options. I'm using "ADO.NET Self-Tracking Entity Generator", but the same way works for all of them. Choosing this adds two template files (Model.tt and Model.Context.tt) to your project, which you are free to modify as you see fit. For the modification you're asking about, you'll find a code generation for your entity classes at or near line 37. Change

<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class ...

to

partial class ...

For complex type there is a similar line further down below: also erase the part before "partial".

This will set the access modifiers for all types at once, future types included. For customizing individual types you better use the model browser.

You will have to set the property "Entity Container Access" of the model itself to internal as well, otherwise members with incompatible access modifiers will be generated.

like image 1
Gert Arnold Avatar answered Oct 12 '22 15:10

Gert Arnold