'PostgresException: 23505: duplicate key value violates unique constraint "PK_Votes"

I am using EF Core 6.0.5. I am getting error when I am trying to insert something to database. Error like below : PostgresException: 23505: duplicate key value violates unique constraint "PK_Votes"

I have two classes

User Class :

    public class User:IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

Vote Class

    public class Vote
    {
        public int VoteId { get; set; }
        public string UserId { get; set; }
        public string TaskId { get; set; }
        public int VotePoint { get; set; }
        public User User { get; set; }
    }

DbContext

    public class ScrumDbContext : IdentityDbContext<User, VoteRole, string> //
    {
        public ScrumDbContext(DbContextOptions<ScrumDbContext> options) : base(options) {}
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.UseSerialColumns();
            base.OnModelCreating(modelBuilder);
        }
        public DbSet<User> Users { get; set; }
        public DbSet<Vote> Votes { get; set; }
    }

And last one, function I got an error.

        public int AddVote(Vote vote)
        {
            _context.Votes.Add(vote);
            var result = _context.SaveChanges(); //I am getting error in this line.
            if (result == 1)
                return 1;
            else return 0;
        }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source