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

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

问题描述

SQL Server表:

  SomeId PK VARCHAR(50)NOT NULL 
OtherId PK诠释不为空

我应该如何在EF 6码首先映射呢?

 公共类的MyTable 
{
[关键]
公共字符串SomeId {搞定;组; }

[关键]
公众诠释OtherId {搞定;组; }
}



我见过一些例子,你必须为每个顺序列,是必需的?



有没有这个地方?


解决方案

您一定要放在列的顺序,否则怎么在SQL Server应该知道哪一个先行?下面是你需要在你的代码做什么:

 公共类的MyTable 
{
[键,列(令= 0)
公共字符串SomeId {搞定;组; }

[键,列(订单= 1)]
公众诠释OtherId {搞定;组; }
}

您也可以看看的这太问题。如果你想要的官方文件,我建议看官方EF网站。 。希望这有助于



编辑:我刚刚发现朱莉·勒曼博客文章链接到各种EF 6善良。你可以找到任何你需要 href=\"http://thedatafarm.com/blog/data-access/the-somewhat-super-secret-msdn-docs-on-ef6/\">。


Sql server table:

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

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?

解决方案

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

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.

EDIT: 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天全站免登陆