Adr*_* K. 20 c# entity-framework naming-conventions foreign-key-relationship navigation-properties
我们假设我们遇到这种情况:
数据库中的表:
Country (id, country_name), Person (id, login), CountryManager (id_country, id_person), CountryStakeholder (id_country, id_person)
如果我们必须从数据库创建模型,使用Entity Framework Database-First,在VS中我们有一个这样的类:
class Country {
int id;
string country_name;
virtual ICollection<Person> Person1; // Navigation Properties
virtual ICollection<Person> Person2; // ---------||----------
}
Run Code Online (Sandbox Code Playgroud)
我已经简化了很多代码,但希望你明白了.
似乎当Entity Framework处理外键时,它会创建通用导航属性.是否有可能控制按名称创建导航属性的方式?不幸的是,Person1,Person2不是很解释.
在 VS 中,您可以使用 GUI 来完成此操作。
如果您显示模型浏览器,则沿树向下导航至:
YourEntityModel > 实体类型 > 国家/地区
然后右键单击“Person1”导航属性并选择“属性”,然后您可以将导航属性名称更改为您喜欢的任何名称:
只需更改名称,保存更改即可完成...
(实际上有很多方法可以进入导航属性属性窗口 - 您也可以在模型图中右键单击它)