如何使用实体框架首先定制外键列名 [英] How to customize foreign key column name using Entity Framework Code-first

查看:115
本文介绍了如何使用实体框架首先定制外键列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型

public class Foo
{
    public int Id { get; set; }
    public IList<Bar> Bars { get; set; }
}

public class Bar
{
    public int Id { get; set; }
}

当我使用EF代码生成数据库模式时,首先创建以下表格:

When I generate the database schema from this using EF Code first it creates the following tables:


  • Id(PK,int,not null)


  • Id(PK, int,not null)

  • Foo_Id(FK,int null)

使用属性更改Foo_Id外键的名称?

Is it possible to change the name of the Foo_Id foreign key using attributes?

编辑:这是我需要的版本:

public class Foo
{
    public int Id { get; set; }

    [ForeignKey("AssignedToBarId")]
    public IList<Bar> Bars { get; set; }
}

public class Bar
{
    public int AssignedToBarId { get; set; }
    public int Id { get; set; }
}


推荐答案

我认为可以完成如下:

或者也许:

[ForeignKey("AssignedToBarID")]
public IList<Bar> Bars { get; set; }

注意:我还没有测试过。这些只是建议。

NOTE: I have not tested neither of them. These are just suggestions.

这篇关于如何使用实体框架首先定制外键列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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