如何体现我的名字实体的导航属性名称的变化 [英] How to reflect the changing of my entities names to the navigation properties names

查看:252
本文介绍了如何体现我的名字实体的导航属性名称的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置实体根据生成的 DB首先中的EntityFramework的工作流程。

I have set of Entities generated according to DB First work flow in EntityFramework.

该实体在适当的名称,所以我决定要改变他们,但是当我改变实体名称,导航性能保持多元化与旧名称的类似下面的单数:两个实体( SREmp ,SRPic

The Entities have in proper names so i decide to change them but when i change the Entity name, the navigation property keeps the pluralization and the singularization of the old name like the following : Two Entities (SREmp ,SRPic)

SREmp      SRPic

Id         id
name       content
phone      note
- - -      - - -
SRPics     SREmp


现在,当我改变我的实体来(员工,EmpPhoto )的名字,
导航性能没有变化!


Now when i change the names of my entities to (Employee,EmpPhoto), the navigation properties didn't change !!

推荐答案

如你所知,当您使用DB第一种方式,您使用的提取从数据库中的信息来创建模型实体T4模板。因此,模型实体会得到他们的名字和属性从DATABSE的信息。

As you know, when you use the DB First approach, you're using a T4 template that extracts the information from the database to create the model entities. So, the model entities will get their names and properties from the information in the databse.

所以,如果你需要自定义属性的名称,只有两种方法可以做到这一点,而且都涉及到自定义模板,T4:

So, if you need to customize the names of your properties, there are only two ways to do it, and both involve customizing the T4 template:


  1. 如果您无法修改数据库以任何方式,因为你并不想这样做,或者您没有权限,通过包括数据库表名映射到所期望的字典修改T4模板实体名称。然后,沿T4模板code,用字典来创建实体的名称,也是导航属性名称。

  1. if you cannot modify your database in any way, because you don't want to do it or you don't have permissions, modify the T4 template by including a dictionary that maps the database table names to the desired entity names. Then, along the T4 template code, use the dictionary to create the entity names, and also the navigation property names.

如果你可以修改DATABSE,加入约所需的实体名称,导航名等信息进行修改。你可以做到这一点在扩展属性,或使用命名约定,例如,外键约束。

if you can modify the databse, modify it by adding information about the desired entity names, navigation names and so on. You can do it in extended properties, or using naming conventions for, for example, the foreign key constraints.

第二溶液可以以几种不同的方式来实现。例如,我使用FK约束名称来定义导航属性。我的名字我的数据库FKS soemthing像 FK_Employee_Pictures ,所以该模板创建在图片表中的导航属性员工,和图片在雇员表。这真的很强大,因为你不能给precise名到你的关系,像 FK_Empoyee_FacePicture Fk_Employee_IdCardPicture 。您还可以使用扩展属性,如描述,从你的T4模板访问它们。您可以定义自己的约定,然后你必须修改T4模板使用它们。

The 2nd solution can be implemented in several different ways. For example, I use the FK constraint names to define the navigation properties. I name my DB FKs soemthing like FK_Employee_Pictures, so that the template create the navigation properties Employee in the pictures table, and Pictures in the employee table. This is really powerful, because you can't give precise names to your relations, with names like FK_Empoyee_FacePicture or Fk_Employee_IdCardPicture. You can also use extended properties, like the descriptions, to access them from your T4 template. You can define your own conventions, and then you have to modify the T4 template to use them.

任何其他可能的解决方案需要人工干预,这是一个非常糟糕的主意。你能找到一个给定的实体的所有使用模型中的项目,并进行了重命名重构。但是,这是昂贵的,而且,如果你修改你的数据库,你需要手动重新更新你的模型实体。使用任何previous解决方案,您只需要更新映射字典,或DB公约,并再次运行T4模板,这是更便宜和可重复得多。

Any other possible solution requires manual intervention, which is a very bad idea. You could find all uses of a given entity in your model project, and make a rename refactor. But that's expensive, and, if you modify your database, you'll need to update you model entities by hand again. Using any of the previous solutions you only have to update the mapping dictionary, or the DB conventions, and run the T4 template again, which is much cheaper and repeatable.

您应该寻找一个powerfu,且易于理解的定制模板。我真的很喜欢这一个:的EntityFramework反向POCO发电机

You should look for a powerfu, and easy to understand an customize template. I really like this one: EntityFramework Reverse POCO Generator.

这篇关于如何体现我的名字实体的导航属性名称的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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