Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a serial field with Postgresql with Entity Framework Code First

I have a serial field (called displayID) that is not the key of the table in a postgresql database and I am working with entity framework code first. When I add new objects into the database through the context, I save the object but it inserts into the database with a value of 0 rather than the next integer. I can't get it to auto-increment the value in the database. I'm using devArt postgres entity framework driver. Is there something special I have to do?

like image 667
GBreen12 Avatar asked Mar 20 '23 08:03

GBreen12


1 Answers

Try adding the Identity Data Annotation to your displayID property. Don't know if the DevArt stuff supports that but it should work.

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

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

}
like image 166
Quesi Avatar answered Apr 25 '23 06:04

Quesi