【问题标题】:Update foreign key convention in EF4更新 EF4 中的外键约定
【发布时间】:2012-02-08 16:33:37
【问题描述】:

我有一个 EF 代码优先数据库。

我有几个实体:

  • 客户和
  • 订单

订单引用客户的地方

IE

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

这生成了一个包含两个表的数据库

  • 客户
  • 订单

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

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

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

我正在使用 SqlServer 2008。

【问题讨论】:

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


    【解决方案1】:

    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...
    }
    

    【讨论】:

    • hrm...我真的希望不是这样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多