Entity Framework Core 2.0 autoincrement and primary key mapping

asp.net-core-2.0 c# entity-framework-core

Question

I am working on converting a database-first project to code-first.

I have the following class and I am using EF Core 2.0:

public class WorkUser
{
    public int WorkUserId { get; set; }

    public int UserId { get; set; }
}

I tried the following and not sure whether it is correct.

public class WorkUser
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int WorkUserId { get; set; }

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

I want the WorkUserId as an autoincrement property and UserId as primary key. The UserId value is coming from another table. So want this to be primary key but without autoincrement.

How to achieve this?

1
2
1/10/2018 12:14:08 PM

Popular Answer

The following worked as i expected:

public class WorkUser
{
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int WorkUserId { get; set; }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int UserId { get; set; }
}
2
1/10/2018 11:45:49 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow