0

I try to add a object to SQL Database with EntityFramework 6, it looks like this :

Table :

    CREATE TABLE [dbo].[jobb](
    [starttid] [datetime] NULL,
    [sluttid] [datetime] NULL,
    [rowversion] [timestamp] NOT NULL,
    [service] [nvarchar](100) NOT NULL,
    [jobb_key] [int] NOT NULL,
CONSTRAINT [PK_service_jobb] PRIMARY KEY CLUSTERED 
(
    [jobb_key] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Class that represent the table :

public class Jobb
{
    public DateTime? starttid { get; set; }
    public DateTime? sluttid { get; set; }
    [Timestamp]
    public byte[] rowversion { get; set; }
    public string service { get; set; }
    [Key]
    public int jobb_key { get; set; }

}

Code that tries to add the new object to database :

            using (var context = new ServiceJobbContext(_settingsService))
        {

            var current = await context.ServiceJobb.FirstOrDefaultAsync(c => c.jobb_key == serviceJobb.jobb_key);

            if (current == null)
                current = context.ServiceJobb.Add(serviceJobb);
            else
            {
                current.service = serviceJobb.service;
                current.sluttid = serviceJobb.sluttid;
                current.starttid = serviceJobb.starttid;
                current.jobb_key = serviceJobb.jobb_key;
            }
            await context.SaveChangesAsync();
            return serviceJobb;
        }

I have checked thatthe jobb_key is set but still I get this exception when running SaveChangesAsync :

SqlException: Cannot insert the value NULL into column 'jobb_key', table 'MyDB.dbo.jobb'; column does not allow nulls. INSERT fails. The statement has been terminated.

Why do I get this exception?

4
  • 1
    This line current.jobb_key = serviceJobb.jobb_key; seems pretty useless as you already get current by its jobb_key being equal to serviceJobb.jobb_key Commented May 15, 2019 at 7:22
  • You are correct, removed, thanks. In this case it is however never running that part of the code, just the Add. Commented May 15, 2019 at 7:33
  • What is the value of serviceJobb.jobb_key on that line current = context.ServiceJobb.Add(serviceJobb);? Commented May 15, 2019 at 7:38
  • current is null so it will try to add the serviceJobb. In serviceJobb the jobb_key is set to 3. Commented May 15, 2019 at 8:13

2 Answers 2

1

your key column [jobb_key] [int] NOT NULL is not null while you are trying to insert null value to it in insertion operation so you should change the key field to be insert able or make it identity to be inserted automatically.

so in this statement it fail to insert

current = context.ServiceJobb.Add(serviceJobb);

solution 1 :

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int jobb_key { get; set; }

to explicit insert it because by default it is DatabaseGeneratedOption.Identity so EF pass it as null

solution 2: make it identity in the level table in DB

 [jobb_key] [int] NOT NULL  IDENTITY(1,1) ,

check this link also of auto generated properties in EF

Sign up to request clarification or add additional context in comments.

6 Comments

Well, public int jobb_key cannot hold null anyway, right?
Im sure that the jobb_key field is set to a int value, I have dubble checked in debug.
entity framework might be assuming that your jobb_key field is an identity and therefore passing it AS NULL
try to add this attribute to you key in the entity [DatabaseGenerated(DatabaseGeneratedOption.None)]
you can check this link learnentityframeworkcore.com/configuration/… hope it will help
|
0

Add the [Required] attribute to the not null Fields and [DatabaseGenerated] to the rowversion:

public class Jobb
{
    public DateTime? starttid { get; set; }

    public DateTime? sluttid { get; set; }

    [Timestamp]
    [DatabaseGenerated]
    public byte[] rowversion { get; set; }

    [Required]
    public string service { get; set; }

    [Key]
    public int jobb_key { get; set; }

}

Note: ValueTypes are implied to be [Required]

4 Comments

Well, public int jobb_key cannot hold null anyway, right?
Tried this, It makes some extra validations and it fails on rowevers(is not set). I need the SQL database to handle the rowversion(set and check).
If I remove ren required on rowversion I yet again get this exception : SqlException: Cannot insert the value NULL into column 'jobb_key', table 'MyDatabase.dbo.jobb'; column does not allow nulls. INSERT fails. The statement has been terminated. And I can see that the jobb_key is set to 3 on the added object.
@Banshee is jobb_key ever modified? Is it 0?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.