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

查看:125
本文介绍了如何在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.

在DVD回答后改编:

设计者可以选择添加代码生成项".如果使用此选项,您将获得几个选项.我正在使用"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 ...

部分课程...

partial class ...

对于复杂类型,下面还有一条类似的线:还删除"partial"之前的部分.

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天全站免登陆