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?
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.
Sure, in Model Browser window select > EntityTypes
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With