首先使用 EF 代码映射复合键 [英] Mapping composite keys using EF code first

查看:30
本文介绍了首先使用 EF 代码映射复合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sql 服务器表:

SomeId PK varchar(50) not null 
OtherId PK int not null

我应该如何首先在 EF 6 代码中映射它?

How should I map this in EF 6 code first?

public class MyTable
{
    [Key]
    public string SomeId { get; set; }

    [Key]
    public int OtherId { get; set; }
}

我看过一些示例,您必须为每列设置顺序,这是必需的吗?

I've seen some examples where you have to set the order for each column, is that required?

是否有关于此的官方文档?

Is there official documentation on this somewhere?

推荐答案

你肯定要放列顺序,不然SQL Server怎么知道哪个先到?以下是您需要在代码中执行的操作:

You definitely need to put in the column order, otherwise how is SQL Server supposed to know which one goes first? Here's what you would need to do in your code:

public class MyTable
{
  [Key, Column(Order = 0)]
  public string SomeId { get; set; }

  [Key, Column(Order = 1)]
  public int OtherId { get; set; }
}

您还可以查看这个问题.如果您需要官方文档,我建议您查看 官方 EF 网站.希望这会有所帮助.

You can also look at this SO question. If you want official documentation, I would recommend looking at the official EF website. Hope this helps.

我刚刚找到了 Julie Lerman 的一篇博客文章,其中包含指向各种 EF 6 优点的链接.您可以在这里.

I just found a blog post from Julie Lerman with links to all kinds of EF 6 goodness. You can find whatever you need here.

这篇关于首先使用 EF 代码映射复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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