7
votes

So i'm trying to do a simple add an entry to my db on azure but for some reason the db is not generating my PK for the entry.
Below is all the code used to create the db and do the entry.
This is on EF 6.1 on a console app

Context

public BlizzardDbContext() : base("AzureSqlDb")
{
}

public DbSet<Maintenance> Maintenances { get; set; }

Model

public class Maintenance
{
    public Maintenance()
    {}

    public Maintenance(DateTime start, DateTime end, string info)
    {
        Start = start;
        End = end;
        Info = info;
    }

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

    public DateTime? Start { get; set; }

    public DateTime? End { get; set; }

    public string Info { get; set; }
}

Test that failed on save changes

var context = new BlizzardDbContext();
context.Maintenances.Add(new Maintenance() {Start = DateTime.Now, End = DateTime.Now, Info = ""});
context.SaveChanges();

I know it sounds so simple, i've used EF a few times before but cannot figure out what is going wrong this time and here's the error

"Cannot insert the value NULL into column 'MaintenanceId', table '.dbo.Maintenances'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."

enter image description here

Update: Ended up fixing this by deleting the db and recreating it, I think there was something weird going on with EF, it wasn't updating the db with the migration properly since after recreating it the column was then set to be Identity

2
The error message pretty much says exactly what the problem is. Either you aren't passing the value because you told EF the database is suppose to supply it, or you are passing the value because the column isn't an identity column. In this case, the column isn't an identity column (or it's been turned off). - Erik Philips
@ErikPhilips Yes I understand that but as you can see from my model, the data annotation (I also tried with fluent API) tells the db that it is the PK collum. this is the SQL generated from accessing the col in SQL Server Object Explorer ALTER TABLE [dbo].[Maintenances] ADD CONSTRAINT [PK_dbo.Maintenances] PRIMARY KEY CLUSTERED ([MaintenanceId] ASC); - Toxicable
That is a SQL Server error. The SQL Server is telling you it's not an identity column regardless of how you've configured EF. - Erik Philips

2 Answers

2
votes

Just verify whether the MaintenanceId is identity key or not in DB. If it is not or you are not sure you can try below option

change

 [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

to

[DatabaseGenerated(DatabaseGeneratedOption.None)]

.None - That means "The database does not generate values."

0
votes

Log on to your database and verify that the MaintenanceId is indeed auto-generating the key. EF supports this, and I suspect that something fun happened during migration, if you used that, or during the construction of the table.

Also, make sure that you have not disabled tracking of objects in your DbContext class. It is default on, so unless you explicitly disabled it, you do not have to worry about that one :)