EF code第4.3命名约定外键
问题说明
我有以下实体:
public class User
{
public int ID {get; set;}
public int GroupID {get; set;} // navigation property with
public Group Group {get; set;} // foreign key field
public Adress Adress {get; set;} // navigation property only
}
从实体框架生成的表如下所示:
The table generated from entity framework looks like:
ID
GroupID
Adress_ID
我不喜欢,该列命名为FK列是不一样的。我能做到这一点都使用相同的约定或者组ID,AdressID或GROUP_ID,Adress_ID?
I don't like, that the column naming for the FK columns is not the same. Can I achieve that both use the same convention either "GroupID, AdressID" or "Group_ID, Adress_ID"?
我使用约定优于配置,并且不希望使用流利的API。
I use convention over configuration and don't want to use Fluent API.
正确答案
EF 4.1至4.3不支持创建自定义的约定,所以这是不可能的。你可以这样做(不流利API)的唯一的事情很可能映射外国财产另一列名:
EF 4.1 to 4.3 doesn't support creating custom conventions, so it's not possible. The only thing you can do (without Fluent API) is probably mapping the foreign property to another column name:
[Column("Group_ID")]
public int GroupID {get; set;}
然后你有两个FK列有下划线在数据库中。但是 - 正如你所看到的 - 你需要覆盖数据注解的惯例至少
Then you have both FK columns with underscore in the database. But - as you can see - you need to overwrite the conventions with data annotations at least.
定义第二FK列没有下划线只能用流利的API:
Defining the second FK column without underscore is only possible with Fluent API:
modelBuilder.Entity<User>()
.HasOptional(u => u.Address)
.WithMany()
.Map(x => x.MapKey("AddressID"));
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfkeic
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20