Navigation Properties on Join Tables in Entity Framework

别说谁变了你拦得住时间么 提交于 2019-12-13 02:29:42

问题


So I have a table in my SQL database:

CompanyRelationships
--------------------
ID
CompanyID
RelatedCompanyID
PermissionGroupID

Which defines when a company allows access to it's records to another company. The "CompanyID" is the company that is granting access, the "RelatedCompanyID" is the company that is getting the access.

Yes, two records could exist for the same two companies (each granting access to the other)

The problem is when I generate the entity model from the db (In Visual Studio 2010), the navigation properties I get are not exactly helpful as to which is which. I get:

Company
Company1
UserGroup

UserGroup is of course obvious, but I would like to know which company reference is which, as that is kind of important. I'm sure I could test it and find out, but then I would have to retest to be sure every time I regenerated from the database. Is there a way to force the naming of the navigation property to be that of the FK field and not the entity it is linked to?


回答1:


You Could always Click on the Navigational Property on the Entity in the Model Browser, and look up the Properties Window. The Window Shows the Association Name (created when adding Table relationships in database). Once you identify the Relation, you could always rename the Navigational Property Name to a name of your choice.

Example, Clicking "Company1" Navigational property in the Entity Diagram would show you a relationship name as "Company_Id_to_Related_Company_Id" (or whatever the name is). Once you know what it means, you can rename the Navigational Property to a meaningful name such as "MasterCompany".




回答2:


Grab this command-line app https://github.com/timabell/ef-edmx-nav-namer, compile it in visual studio, and run it against your edmx file (take a backup first!) like this:

EfEdmxNavNamer.exe -i path\to\your\Model.edmx

For the following key name: FK_Parent_Child it will set the navigation properties at each end as follows:

  • on the parent entity: "Child"
  • on the child entity: "Parent"

This only really covers the most basic case, but it's something. Pull requests welcome if you want to improve on it. It's Apache 2.0 licensed.



来源:https://stackoverflow.com/questions/6203308/navigation-properties-on-join-tables-in-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!