Using Entity Framework with code-first no autoincrement
Asked Answered
P

3

6
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.

Puppet answered 20/3, 2019 at 20:1 Comment(3)
You mention ASP.NET MVC 5 - but you also says it's a WInforms application - which is it now ?? I can't be both at the same time.....Kaufmann
ow whoops that's wrong it's a winform applicationPuppet
That it is winforms probably has nothing to do with your problem. I think it is in your dbContext. If you move these classes and your dbContext to a simple console app, do you get the same problem? Do you do something special in your dbContext? Can you edit the question: add dbContext and show how you add a Movie to the database?Cola
T
8

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.

Totten answered 20/3, 2019 at 20:7 Comment(12)
is that new then? because i have another application where those attributes arent there and that application works perfectly. but thanks .Puppet
This may be EF version specific issue then. You can check versions in those different applications.Totten
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 errorsPuppet
i might find an answer here https://mcmap.net/q/1773620/-auto-increment-on-partial-primary-key-with-entity-framework-core . 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 truePuppet
DatabaseGeneratedOption.Identity is for autoincrement. While DatabaseGeneratedOption.None is for primary key but without auto increment. So this answer should help i guess. !Totten
i started with .identity but didn't work. I even deleted my database and did update-database again just to be surePuppet
Did you generate the migrations again ?Totten
Let us continue this discussion in chat.Puppet
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.Fleischer
The attribute is not needed because that's the default conventional behavior of int type PKs. As @Fleischer mentioned, the migrations and database must be updated to reflect the model.Cyclades
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;Puppet
well apparently it worked. I deleted the migrations folder then again enable-migration after that add-migration and upddate-database.Puppet
B
1

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

Bistre answered 20/3, 2019 at 20:48 Comment(2)
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 :(Puppet
By the way, next time try not to make your answer a link-only answer. Please post any relevant text in your answer.Mho
F
0

When I generated my table using code first, the definition of my primary key on my table is data type

double

So when the table was generated, the identity was not present on the primary key.

So what I did is reverse the migration, and change the data type to

integer

In this case, the auto-increment or identity on the table was generated.

Featly answered 21/2, 2020 at 20:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.