19

When trying to use this code:

var model = new MasterEntities();

var customer = new Customers();
customer.Sessionid = 25641;

model.Customers.Add(customer);
model.SaveChanges();

I get:

{"Cannot insert the value NULL into column 'Sessionid', table 'master.dbo.Column'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

The column "Sessionid" is actually the primary key and is marked with [KEY] like this:

 public class Customers
    {   
        [Key]
        public long Sessionid { get; set; }
        public long? Pers { get; set; }
    }

So according to this question, it seems as if when the property is marked with [KEY], EF ignores my own declaration of Sessionid since it expects the database to assign the value.

So how can I solve this? If I remove [KEY] I get the "entity type has no key defined" exception...

Community
  • 1
  • 1
Dimo
  • 3,158
  • 6
  • 24
  • 45
  • Is your table *actually* called `Column`? And in the `master` database? – Martin Smith Nov 01 '13 at 13:06
  • 2
    possible duplicate of [Using Entity Framework Code First CTP5, how do I create a primary key column that are INTs and are not identity columns](http://stackoverflow.com/questions/4999853/using-entity-framework-code-first-ctp5-how-do-i-create-a-primary-key-column-tha) – CodeCaster Nov 01 '13 at 13:07
  • Maybe your SessionID has autoIncrement? – Arthur Nov 01 '13 at 13:08
  • I agree with @Martin.I think something went wrong when mapping database and classes. – doganak Nov 01 '13 at 13:09
  • No @MartinSmith it's not :) I just edited it to make it more clear. – Dimo Nov 01 '13 at 13:23

3 Answers3

38

I solved it by adding [DatabaseGenerated(DatabaseGeneratedOption.None)] like this:

public class Customers
    {   
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long Sessionid { get; set; }
        public long? Pers { get; set; }
    }
Dimo
  • 3,158
  • 6
  • 24
  • 45
  • I can not find out dll for DatabaseGenerated – John Nguyen Aug 18 '14 at 03:53
  • @JohnNguyen `DatabaseGenerated` and `DatabaseGeneratedOption` are both in the `System.ComponentModel.DataAnnotations.Schema` namespace, which is part of EntityFramework. Make sure you have a reference to EF or have the EF NuGet package installed. – Nate Barbettini Dec 19 '14 at 15:02
  • Brilliant - thank you sir! Been trying different variations of this for 4 hours until I came across your excellent solution! – ma11achy May 08 '15 at 14:38
2

You can configure SQL to auto-generate (and auto-increment) the primary key for the table upon inserts. Then just remove the [Key] in C# and you don't need to set the ID in the application manually, the db will generate it for you.

Code Monkey
  • 643
  • 6
  • 18
  • Is there a specific reason why you would like to do that? From a design point of view, that is not 'generally' the norm - you can still let SQL generate a generic ID column as the pKey and set your own 'sessionID' manually as though it were a regular (non-key) column..? – Code Monkey Nov 01 '13 at 13:28
  • yes there is a specific reason :) But too difficult to get into that here in a comment :) – Dimo Nov 01 '13 at 13:33
  • 1
    Sounds like the kind of thing Dean Varillas would often do.. led me to quit. – Code Monkey Sep 08 '15 at 00:03
1

I have encountered this problem multiple times while working with Microsoft SQL Server and I have followed the same way to fix it. To solve this problem, make sure Identity Specification is set to Yes. Here's how it looks like:

Identity Specification

In this way the column number auto increments as a primary key normally would.

HOW?: right-click the table that contains the column, choose Design, select the primary key and in Column Properties window find Identity Specification and set it to Yes.