11

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?

4 Answers 4

4

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.

4
  • I am aware of the Properties window, but it does not allow me to select multiple entities and set their access modifier at the same time. I have to do it one by one. I guess I need to look at the T4 templates. Mar 1, 2012 at 12:36
  • 2
    Open the EDMX file with XML editor (it's a plain XML file) and do the batch replace there. Done in 20 seconds. Mar 1, 2012 at 20:45
  • Yeah, I was going for some more permanent solution (where the modifiers would not be replaced by code regeneration), but that's ok too. Mar 1, 2012 at 21:28
  • 1
    It will not. If you replace it in XML, it's going to stay there even after regenerating the model. It's the same as doing it in Model Browser window, just faster. Mar 2, 2012 at 9:12
2

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.

1

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.

0

I wanted my entity container as well as the generated complex classes to be internal. First I set the "Entity Container Access" to internal. Next I added a container variable to the top of the "Model.tt" file after the initialization of the itemCollection variable. I found the container variable code in the "Model.Context.tt" file.

    var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);
    var container = itemCollection.OfType<EntityContainer>().FirstOrDefault();

Next I changed

    <#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#>

to

    <#=Accessibility.ForType(container)#> partial class <#=code.Escape(complex)#>

I chose to modify the code in this way so that I could keep my complex class container access modifiers in sync with the container access modifier.

Thank you Gert Arnold for leading me in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.