更新 EF4 中的外键约定

发布于 2025-01-03 16:48:44 字数 722 浏览 5 评论 0原文

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

我有几个实体:

  • 客户和
  • 订单

,其中订单引用客户

IE

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

数据库

  • 这生成了一个包含两个表客户
  • 订单的

,其中订单有一个字段 Customer_id 我需要将此字段重命名为 客户 ID。

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

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

我正在使用 SqlServer 2008。

I've got a EF code first database.

I've got a couple of entities:

  • customer and
  • orders

where Orders references Customer

IE

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

This generated a DB with two tables

  • Customer
  • Orders

where Orders has a field Customer_id I need to have this field renamed to CustomerId.

I know I can map it, but I'd like this to be a convention.

I've looked at this page http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.conventions%28v=vs.103%29.aspx but I'm not clear on what these conventions all mean.

I'm using SqlServer 2008.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏夜暖风 2025-01-10 16:48:44

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

Customer_id is generated by EF convention.

You have to declare a foreign key and use data annotation to associate it.

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...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文