28

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.

5
  • What is the type of id? Could be it that something is going wrong with generating the key in the database? Commented Oct 22, 2011 at 8:15
  • type is bigint in sqlserver, in User class it is long.. Also when I use normal entity framework without codefirst, it works Commented Oct 22, 2011 at 9:04
  • 4
    How did you map the user? EF code first by default expects that Id value is generated in the database (by Identity settings). Commented Oct 22, 2011 at 10:32
  • My User Class has two fields of which Id has [key] attribute. In db I have made Id primary key but have stated IsIdentity =no , so the value is not going to be generated in the db. @Ladislav : what u r saying may be the problem, how do I solve this? I can not make isIdentity=yes Commented Oct 22, 2011 at 11:07
  • ok, thanks to Ladislav to point me in the right direction: Adding the attribute [DatabaseGenerated(DatabaseGeneratedOption.None)] solved the problem. Commented Oct 22, 2011 at 11:40

3 Answers 3

24

Referring to this post it seems that entity framework expects by default that you insert into identity column.

To solve this try:

modelBuilder.Entity<BOB>() .HasKey(p => p.Id) .Property(p => p.Id) .StoreGeneratedPattern = StoreGeneratedPattern.None; builder.Entity<BOB>().MapSingleType().ToTable("BOB"); 

or decorate your key in the POCO with:

[Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] //Fixed typo public Int64 PolicyID { get; set; } 
Sign up to request clarification or add additional context in comments.

2 Comments

I believe the enum flag for that attribute is "DatabaseGeneratedOption" and not "DatabaseGenerationOption"
It worked for me too can any one explain why it happened how this attribute solved the issue.
7

I ran into the same problem, found your question, and then noticed this Entity Framework 4.1 code-first KeyAttribute as non-identity column question ( which has an attribute that solved my problem ).

If you define User as:

public class User { [DataMember, Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)] public long ID { get; set; } [DataMember] public string Email { get; set; } } 

The key here is putting the attributes:

  • Key
  • DatabaseGenerated

onto the ID column. Apparently the issue we are fighting is that Entity Framework by default expects to do inserts with keys being identities.

Comments

2

I fixed this by setting StoreGeneratedPattern on the column properties to Computed.

Step by step below:

  1. Open your EDMX (double click in Visual Studio)
  2. Right click on the column that causes the problem, select properties.
  3. Change StoreGeneratedPattern to be Computed.

From the IDE enter image description here

Hope that helps somebody.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.