Why is EF trying to insert NULL in id-column?

前端 未结 9 1985
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 00:43

I am writing my project using Entity Framework 4.0 (Model first). At the beginning of the project, I faced with this problem: I am trying to insert the filled object in the

相关标签:
9条回答
  • 2020-12-10 01:06

    Have a look at this: https://stackoverflow.com/a/5338384/171703 - entity framework might be assuming that your CategoryId field is an identity and therefore passing null to the database expecting it to fill it for you.

    0 讨论(0)
  • 2020-12-10 01:12

    I know I'm a bit late but here's how I managed to fix it.

    1. I included these attributes to my int PK in my models. (to give way for my seed in configuration.cs) [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.None)]

    2. After I've inserted my seed I use the SQL Server Object Explorer in Visual Studio and went to the table of this model by right clicking the table and press 'View Designer'

    3. Below you will see the script generated for creating that table. Insert this command right after your primary key property.

      IDENTITY(1,1)

    1. Apply script changes by clicking 'Update' just above the table view (left corner)

    2. After updating the database, go back to your model and change this attribute:

      [DatabaseGenerated(DatabaseGeneratedOption.None)] to [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    3. Save, Add migration then Update Database should do it.

    Everytime you insert, it will now increment depending on what you specified on the IDENTITY script.

    0 讨论(0)
  • 2020-12-10 01:17

    Met the same problem today.

    Here is how I figured out the issue.

    I had added the identity seed to the column initially but after that I removed it. So I did not realize that when modifying the column definition in the Code first, the Id

    Property(x => x.Id)
        .HasColumnName("Id")
        .HasColumnType("int")
        .IsRequired();
    

    So Entity framework figured out that it was an Identity column and I got the exception above.

    To fix this, I added the following:

    HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
    

    so the final piece was something like:

    Property(x => x.Id)
        .HasColumnName("Id")
        .HasColumnType("int")
        .IsRequired()
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
    0 讨论(0)
  • 2020-12-10 01:23

    Try to add this into your model class .cs file:

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int CategoryId { get; set; }
    

    Or change your column CategoryId to identity:

        CategoryId int IDENTITY(1,1)
    
    0 讨论(0)
  • 2020-12-10 01:23

    For me I had a different schema other than default one. The name of the Id key was some how missing the that schema part - fixed that in the database and it all went well. How it was ;

    PK_TableName
    

    How I changed it to

    PK_mySchema.TableName
    
    0 讨论(0)
  • 2020-12-10 01:28

    İf Model first try block

    [Key]
     [DatabaseGenerated(DatabaseGeneratedOption.None)]
     public Int64 PolicyID { get; set; }
    
    0 讨论(0)
提交回复
热议问题