How to make all entities access:internal instead o

2020-03-12 07:19发布

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条回答
你好瞎i
2楼-- · 2020-03-12 07:29

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.

查看更多
可以哭但决不认输i
3楼-- · 2020-03-12 07:29

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.

查看更多
爷的心禁止访问
4楼-- · 2020-03-12 07:35

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.

查看更多
家丑人穷心不美
5楼-- · 2020-03-12 07:39

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.

查看更多
登录 后发表回答