Using Entity Framework with code-first no autoincrement
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
Technology Researcher
Updated on June 07, 2022Comments
-
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 over 5 yearsis that new then? because i have another application where those attributes arent there and that application works perfectly. but thanks .
-
Manoj Choudhari over 5 yearsThis may be EF version specific issue then. You can check versions in those different applications.
-
Technology Researcher over 5 yearsOke 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 over 5 yearsnope 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 over 5 yearsi 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 over 5 yearsDatabaseGeneratedOption.Identity is for autoincrement. While DatabaseGeneratedOption.None is for primary key but without auto increment. So this answer should help i guess. !
-
Technology Researcher over 5 yearsi started with .identity but didn't work. I even deleted my database and did update-database again just to be sure
-
Manoj Choudhari over 5 yearsDid you generate the migrations again ?
-
Technology Researcher over 5 yearsLet us continue this discussion in chat.
-
Doug F over 5 yearsBy the way, next time try not to make your answer a link-only answer. Please post any relevant text in your answer.
-
DevilSuichiro over 5 yearsDatabaseGeneratedOption.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 over 5 yearsThe 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 over 5 yearsfor 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 over 5 yearswell apparently it worked. I deleted the migrations folder then again enable-migration after that add-migration and upddate-database.