This is my code, very simple...

 var newUser = new User();
        newUser.Id=id;
        newUser.Email = email;
       this.DataContext.Set<User>().Add(newUser);
        this.DataContext.SaveChanges();

The error I get is a sqlexception at this.DataContext.SaveChanges(); stating that:

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

I have debugged and found that there is value in Id & Email in newUser at this.DataContext.Set<User>().Add(newUser);

If this is the case, how is the value becoming null?

The error stack trace:

[DbUpdateException: An error occurred while updating the entries. See the inner exception for details.]
   System.Data.Entity.Internal.InternalContext.SaveChanges() +204
   System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +23
   System.Data.Entity.DbContext.SaveChanges() +20

I have not been able to understand or solve this....

Would sincerely appreciate any help in this...

Regards Arnab

Solution

Ok, thanks to Ladislav to point me in the right direction: Adding the attribute [DatabaseGenerated(DatabaseGeneratedOption.None)] solved the problem.

Edit
Report