替换实体映射中的下划线
当使用 EF4 创建模型时,我想替换所有下划线以及表/列前缀,以便模型读取清晰。我们的数据库有一个类似的结构:
Table:ABC_Customer
-Column:ABC_Customer_Id
-Column:Name
-Column:ABC_Customer_Type_Id
Table:ABC_Customer_Type
-Column:ABC_Customer_Type_Id
-Column:Name
我想要像这样的实体名称:
Entity:Customer
-Property:CustomerId
-Property:Name
-Property:CustomerTypeId
-NavigationProperty:CustomerType
等等。
生成 edmx 文件时,EF 设计者会按照数据库中显示的方式准确命名所有实体。我知道可以使用 T4 模板修改此行为,但我刚刚发现我可以重命名设计器中的实体,这会在 edmx 文件中生成 EntitySetMapping 元素,因此 T4 似乎有点矫枉过正。看起来我只想对 edmx 文件进行后处理,而不使用 T4 模板,因为在替换之后,我想要默认行为。哪种方法最合适?为什么?
When using EF4 to create a model, I would like to replace all underscores as well as table/column prefixes so that the model reads cleanly. Our database has a structure something like:
Table:ABC_Customer
-Column:ABC_Customer_Id
-Column:Name
-Column:ABC_Customer_Type_Id
Table:ABC_Customer_Type
-Column:ABC_Customer_Type_Id
-Column:Name
I would like to have entities names like so:
Entity:Customer
-Property:CustomerId
-Property:Name
-Property:CustomerTypeId
-NavigationProperty:CustomerType
and so on and so on.
When generating the edmx file, the EF designer names all the entities exactly as they appear in the database. I know this behavior can be modified using the T4 templates, but I just discovered that I can rename the entities in the designer, which generates EntitySetMapping elements within the edmx file, so T4 seems like overkill. It almost seems like I just want to post-process the edmx file, without using the T4 templates since after that replacement, I want the default behavior. Which approach is the most approriate and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
EDMX 负责映射,任何新名称都应在 EDMX 中的映射 = 中定义。
EDMX is responsible for mapping and any new names should be defined in mapping = in EDMX.
除非我要处理非常大数量的表,否则我会使用开箱即用的功能。为了让 T4 模板完全满足您的需要而投入时间可能不值得。
对于小型数据库,如果进行数据库优先开发,我会坚持使用设计器。
或者,您可以先编写代码来获得您想要的干净模型,将其指向现有数据库并处理模型中每个对象的代码映射:
然后应用您的自定义配置(即映射)到模型,同时更改表映射:
我使用它的次数越多,我就越喜欢代码优先。 Julie Lerman 的新书编程实体框架:代码优先很棒!
I would use the out of the box functionality unless I was dealing with a very large number of tables. The time investment to get the T4 templates to do exactly what you need may not be worth it.
For a small database I would stick with the designer if doing database first development.
Alternatively, you could go code first to get a clean model like you want, point it at your existing database and handle the mappings in code for each object in the model:
And then apply your custom configurations (i.e. mappings) to the model at the same time you change the table mappings:
The more I work with it, the more I like code first. Julie Lerman's new book Programming Entity Framework: Code First is great!