• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

EF code第4.3命名约定外键

用户头像
it1352
帮助1

问题说明

我有以下实体:

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.

正确答案

#1

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
系列文章
更多 icon
同类精品
更多 icon
继续加载