how to set NAME for “multi fields” primary keys (C

2020-08-01 07:19发布

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.

enter image description here

1条回答
走好不送
2楼-- · 2020-08-01 07:50

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.

查看更多
登录 后发表回答