3

Let me show the error:

Cannot insert the value NULL into column 'Id', table 'TCCDatabase.dbo.ProfessionalModels'; column does not allow nulls. INSERT fails.

I got this error when trying to insert n record into my database using a database created by my EF application.

I think it's happening because my database Id column is not auto-incrementing.

Identity field

My question is: Why are my Id Columns not auto-incrementing?

One of my models:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace TCCApplication.Models
{
    public class ProfessionalModel
    {
        public int Id { get; set; }

        public string Name { get; set; }

        [Column(TypeName = "DateTime2")]
        public DateTime BirthDate { get; set; }
        public int PhoneNumber { get; set; }
        public string Profession { get; set; }

        public UserAddressModel UserAddressModel { get; set; }
        public UserAccountModel UserAccountModel { get; set; }

        public ProfessionalModel() { }
    }
}

My context class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;
using TCCApplication.Models;

namespace TCCApplication.EntityFramework
{
    public class TCCDatabase : DbContext
    {
        public DbSet<UserAccountModel> UserAccountContext { get; set; }
        public DbSet<ProfessionalModel> ProfessionalContext { get; set; }
        public DbSet<UserAddressModel> UserAddressContext { get; set; }

        public TCCDatabase()
        {}

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ProfessionalModel>().Property(a => a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<UserAccountModel>().Property(a => a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<UserAddressModel>().Property(a => a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        }

    }
}
Chris
  • 1,533
  • 2
  • 17
  • 33
guisantogui
  • 4,017
  • 9
  • 49
  • 93
  • The definition of your pk should set identity specification to yes and autoinc even without the fluent configuration. What happens if you remove the fluent configuration and build a new migration? – user3411327 Apr 05 '14 at 22:49
  • @user3411327 It creates an empty migration – guisantogui Apr 06 '14 at 12:47
  • Identity must be set on table create. If this did not happen there is an explanation how to intoduce it later [here](http://romiller.com/2013/04/30/ef6-switching-identity-onoff-with-a-custom-migration-operation/). – user3411327 Apr 06 '14 at 17:47

1 Answers1

1

Based on this question, it seems like it is not EF's job to auto-increment the ID, but rather the database's job. The column that should increment needs be set as the identity.

enter image description here

Community
  • 1
  • 1
contactmatt
  • 18,116
  • 40
  • 128
  • 186