如何为“多字段”设置NAME实体框架中的主键(约束主键)? [英] how to set NAME for "multi fields" primary keys (Constraint Primary Key) in entity framework?

查看:155
本文介绍了如何为“多字段”设置NAME实体框架中的主键(约束主键)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实体框架中如何设置多字段主键(约束主键)的名称?
例如:

  // CONSTRAINT PKRoles PRIMARY KEY(Rolename,ApplicationName)

我可以只执行部分代码:



sql cod:



Rolename Text(255)NOT NULL,
ApplicationName Text(255 )NOT NULL,
CONSTRAINT PKRoles PRIMARY KEY(Rolename,ApplicationName)

实现(转换为实体):

  public class Roles 
{
[Key,Column(Order = 0)]
public string Rolename {get;组; } //(255)NOT NULL

[Key,Column(Order = 1)]
public string ApplicationName {get;组; } //(255)NOT NULL

// CONSTRAINT PKRoles PRIMARY KEY(Rolename,ApplicationName)
}

我在数据库的关键部分看到,用 PK_dbo.Roles 命名创建密钥!
但我需要创建密钥 PKRoles neme!



请帮助我实现上面的代码。

非常感谢。



解决方案

以下代码是否执行您想要的?

  public class Roles 
{
[Key,Column(PKRoles,Order = 0)]
public string Rolename {get;组; } //(255)NOT NULL

[Key,Column(PKRoles,Order = 1)]
public string ApplicationName {get;组; } //(255)NOT NULL
}

您要查找的词是复合键,您可能可以使用流畅的API。这可能有助于您的搜索,如果这不起作用。


How to set name for "multi fields" primary keys (Constraint Primary Key) in entity framework?? for example :

// CONSTRAINT PKRoles PRIMARY KEY (Rolename, ApplicationName)

i can implement only part of the code :

sql cod:

CREATE TABLE Roles
(
    Rolename Text (255) NOT NULL,
    ApplicationName Text (255) NOT NULL,
    CONSTRAINT PKRoles PRIMARY KEY (Rolename, ApplicationName)
)

implement (convert for entity):

public class Roles
{
    [Key, Column(Order = 0)]
    public string Rolename { get; set; } //(255) NOT NULL

    [Key, Column(Order = 1)]
    public string ApplicationName { get; set; } //(255) NOT NULL

    //CONSTRAINT PKRoles PRIMARY KEY (Rolename, ApplicationName)
}

i see in keys section of database , that create key with "PK_dbo.Roles" name ! but i need create key "PKRoles " neme!

please help me to implement end line above code.
thanks a lot.

解决方案

Does the code below do what you want?

public class Roles
{
    [Key, Column("PKRoles", Order = 0)]
    public string Rolename { get; set; } //(255) NOT NULL

    [Key, Column("PKRoles", Order = 1)]
    public string ApplicationName { get; set; } //(255) NOT NULL
}

The word you are looking for is "composite key" and you might be able to use the fluent API. That might help your search if this does not work.

这篇关于如何为“多字段”设置NAME实体框架中的主键(约束主键)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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