Using Entity Framework with code-first no autoincrement

11,004

Solution 1

You will have to put below two attributes on ID column

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

As per this blog:

If you forget to mention [Key] , assuming you have made it not null, and explicitly say > Id in C# code, EF will try to pass NULL since its an identity and will throw an exception “Cannot insert the value NULL into column……….“, so can just modify DatabaseGeneratedOption.Identity to DatabaseGeneratedOption.None – which might not fulfill the auto-increment need. So, just keep [Key] and let DB generator to fill it for you. This is the approach when it comes to concurrency.

I hope this answers your query.

Solution 2

Following article explains the use of Key attribute in EF 6 and EF Core.

http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.aspx

Share:
11,004
Technology Researcher
Author by

Technology Researcher

Updated on June 07, 2022

Comments

  • Technology Researcher
    Technology Researcher about 2 years
    public class Movie
    {
        public int Id { get; set; }
    
        [Required]
        [StringLength(255)]
        public string Name { get; set; }
    
        public virtual IList<Genre> Genre { get; set; }
        public virtual IList<Cast> cast { get; set; }
    
        [Display(Name = "Release Date")]
        public DateTime ReleaseDate { get; set; }
    }
    
    public class Genre
    {
        public int Id { get; set; }
    
        [Required]
        [StringLength(255)]
        public String Name { get; set; }
    }
    

    These are 2 of my models. They work perfectly well, except auto increment isn't implemented on the primary key.

    I'm getting error of the id being null and not be able to insert into the database which seems logic to me. Any ideas why there aren't any auto increments and secondly how can I add them? I'm using code-first migration in .NET Framework 4.6, it's a Winforms application.

  • Technology Researcher
    Technology Researcher over 5 years
    is that new then? because i have another application where those attributes arent there and that application works perfectly. but thanks .
  • Manoj Choudhari
    Manoj Choudhari over 5 years
    This may be EF version specific issue then. You can check versions in those different applications.
  • Technology Researcher
    Technology Researcher over 5 years
    Oke this is weird same problem cannot insert null into id. I have added both attribute in all the classes: Movie, Genre, Cast. altough the error came from Cast . When I look into microsoft sql server manager it says id, Pk, int, not null.. I have read the blog and don't know why it is still giving errors
  • Technology Researcher
    Technology Researcher over 5 years
    nope primary key and stuff is working properly it's only auto increment. even when I try to insert manually in microsoft sql manager it also says cannot insert null into id so there is definitly not an auto increment :(
  • Technology Researcher
    Technology Researcher over 5 years
    i might find an answer here stackoverflow.com/a/49711948/8428227 . the only problem is the sql code you see there i cannot add this when using code-first right? and or how can I check this. When modifying id column i already says increment 1 seed 1 identity true
  • Manoj Choudhari
    Manoj Choudhari over 5 years
    DatabaseGeneratedOption.Identity is for autoincrement. While DatabaseGeneratedOption.None is for primary key but without auto increment. So this answer should help i guess. !
  • Technology Researcher
    Technology Researcher over 5 years
    i started with .identity but didn't work. I even deleted my database and did update-database again just to be sure
  • Manoj Choudhari
    Manoj Choudhari over 5 years
    Did you generate the migrations again ?
  • Technology Researcher
    Technology Researcher over 5 years
  • Doug F
    Doug F over 5 years
    By the way, next time try not to make your answer a link-only answer. Please post any relevant text in your answer.
  • DevilSuichiro
    DevilSuichiro over 5 years
    DatabaseGeneratedOption.Identity is the wrong one. Identity tells EF that the DBMS is going to generate the PK value, so it shouldn't (or rather: mustn't) send the PK value it got. However, your DBMS does not create the PK values, but needs a PK for the newly added row. Either re-create the database to have the column have proper autoincrement, or use DatabaseGeneratedOption.None and provide your own PK's. The [Key] data annotation should not be necessary though, since any Property named Id will be mapped as PK as per code first conventions.
  • Ivan Stoev
    Ivan Stoev over 5 years
    The attribute is not needed because that's the default conventional behavior of int type PKs. As @DevilSuichiro mentioned, the migrations and database must be updated to reflect the model.
  • Technology Researcher
    Technology Researcher over 5 years
    for each change in one of the classes i made a migration and did update-database. i even tried to delet the full database and then again update-database;
  • Technology Researcher
    Technology Researcher over 5 years
    well apparently it worked. I deleted the migrations folder then again enable-migration after that add-migration and upddate-database.