Banshee
Banshee

Reputation: 15837

Entityframework replaces field with null and throws exception?

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?

Upvotes: 0

Views: 166

Answers (2)

NO TO OPENAI
NO TO OPENAI

Reputation: 685

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]

Upvotes: 0

Ahmed Yousif
Ahmed Yousif

Reputation: 2348

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

Upvotes: 1

Related Questions