如何使所有实体访问:EDMX 中的内部而不是公共? [英] How to make all entities access:internal instead of public in EDMX?

查看:34
本文介绍了如何使所有实体访问:EDMX 中的内部而不是公共?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的实体框架模型生成具有 internal 访问修饰符的实体,而不是 public.我在库中使用 EF 模型,并且我只想从外部访问单个类(某些控制器).

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.

有没有什么简单的方法可以让 EF 模型生成使用 internal 修改器而不是 public,包括更新时的模型重新生成?

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

推荐答案

这很像我在类似问题上得到的答案.但在您的情况下,您想要自定义类的访问修饰符,而不是 ObjectContext.

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.

根据 hvd 的回答改编:

Adapted after hvd's answer:

设计者有一个选项添加代码生成项".如果您使用它,您将获得多个选项.我正在使用ADO.NET 自跟踪实体生成器",但同样的方式适用于所有这些.选择此项会向您的项目添加两个模板文件(Model.tt 和 Model.Context.tt),您可以根据需要自由修改.对于您所询问的修改,您会在第 37 行或附近找到实体类的代码生成.更改

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))#>部分类...

<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>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.

这篇关于如何使所有实体访问:EDMX 中的内部而不是公共?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆