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

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

问题描述

Sql server表:

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

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

  public class MyTable 
{
[Key]
public string SomeId {get;组;

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

我看过一些例子,你必须为每个列是否需要?



有没有官方的文件在这个地方?

解决方案>

你肯定需要放入列顺序,否则SQL Server应该如何知道哪一个首先出现?以下是您需要在代码中执行的操作:

  public class MyTable 
{
[列(Order = 0)]
public string SomeId {get;组;

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

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



编辑:我刚刚从Julie Lerman找到一篇博客文章,链接到各种EF 6的善良。你可以找到任何你需要的这里


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