更新EF4中的外键约定

时间:2012-02-08 16:33:37

标签: .net entity-framework entity-framework-4

我有一个EF代码第一个数据库。

我有几个实体:

  • 客户和
  • 订单

其中Orders引用Customer

IE

public class Orders
{
    public Guid Id {get; set;}
    public Customer Customer {get; set}
    //snip...
}

这生成了一个带有两个表的数据库

  • 客户
  • 订单

订单中有一个字段 Customer_id 我需要将此字段重命名为 CustomerId

我知道我可以映射它,但我希望这是一个惯例。

我看过这个页面http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.conventions%28v=vs.103%29.aspx,但我不清楚这些约定的含义。

我正在使用SqlServer 2008。

1 个答案:

答案 0 :(得分:3)

Customer_id由EF惯例生成。

您必须声明一个外键并使用数据注释来关联它。

using System.ComponentModel.DataAnnotations;

public class Orders
{
    public Guid Id {get; set;}
    public Guid CustomerId  {get; set;}
    [ForeignKey("CustomerId")]
    public Customer Customer {get; set}
    //snip...
}