There are tons of questions mentionning this error, I went through each one I found but they didn't correspond to my issue. Most of the time in the questions I found, the problem comes from the fact that the author is willing to manually give an ID to an entity but forgets to the switch the Autogenerated option to off.
My problem is exactly the opposit. I have several tables all containing a ID column which is automatically incremented by the database. The CRUDs operations are working fine on all of them but one.
I'm getting the terrible exception which is mentionned in the Title. I've spent 2 hours on this but I can't figure out why. Everything seems just fine.
Here is my model :
public class House
{
public int ID { get; set; }
public string Name {get;set;}
public DateTime CreationDate { get; set; }
public int CompanyID { get; set; }
public virtual BuildingCompany Assignation { get; set; }
public int? NoteID { get; set; }
public Note Note { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
public int? FileID { get; set; }
public virtual File File { get; set; }
}
Here is the mapping code
modelBuilder.Entity<House>().HasKey(r => r.ID);
modelBuilder.Entity<House>().Property(r => r.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<House>().HasRequired(r => r.Company).WithMany(a => a.Houses).HasForeignKey(r => r.CompanyID).WillCascadeOnDelete(false);
modelBuilder.Entity<House>().HasOptional(r => r.File).WithMany().HasForeignKey(r => r.FileID);
modelBuilder.Entity<House>().HasRequired(r => r.User).WithMany().HasForeignKey(r => r.UserID);
modelBuilder.Entity<House>().HasOptional(r => r.Note).WithRequired(n => n.House);
And here is the code I use to try to persist an entity (add a row) :
House house = new House
{
ID=0, // I also tried to remove this line
Name="Nice House",
CompanyID= // Some Integer,
CreationDate=DateTime.Now,
UserID= // Some Integer
};
context.Houses.Add(house)
context.Save();
This makes no sense to me, I've tried to debug and when my code hits the Save Method of my context, the Local cache contains the entity that I've created with the right parameters but then it throws me this error :
Error Cannot insert the value NULL into column 'ID', table 'Houses' column does not allow nulls.
Just as a reminder, I don't need/want to set the ID myself. I'm expecting that EF does it for me as it does with my other tables. That's why this problem particularly puzzles me.
Thanks for your help
Edit
Here is the Database Schema. The Database is auto-generated by EF. I only do Automatic Migrations after every model change. I don't touch at this level of detail.
CREATE TABLE [dbo].[Houses] (
[ID] INT NOT NULL,
[Name] NVARCHAR (MAX) NULL,
[CreationDate] DATETIME NOT NULL,
[CompanyID] INT NOT NULL,
[NoteID] INT NULL,
[UserID] INT NOT NULL,
[FileID] INT NULL,
CONSTRAINT [PK_dbo.Houses] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [FK_dbo.Houses_dbo.Files_FileID] FOREIGN KEY ([FileID]) REFERENCES [dbo].[Files] ([ID]),
CONSTRAINT [FK_dbo.Houses_dbo.Users_UserID] FOREIGN KEY ([UserID]) REFERENCES [dbo].[Users] ([ID]) ON DELETE CASCADE,
CONSTRAINT [FK_dbo.Houses_dbo.BuildingCompanies_CompaniesID] FOREIGN KEY ([CompanyID]) REFERENCES [dbo].[BuildingCompanies] ([ID])
);
GO
CREATE NONCLUSTERED INDEX [IX_CompanyID]
ON [dbo].[Houses]([CompanyID] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_UserID]
ON [dbo].[Houses]([UserID] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_FileID]
ON [dbo].[Houses]([FileID] ASC);
how is your database crearted ? are you sure the column is set to identity on the server ?
if not try to drop the database and re-run your app.