3

I have the following model:

Services.StradaDataReview2Model.UOSChangeLog:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

public int Accident_nr { get; set; }

public int Refnr { get; set; }

public int Action { get; set; }

public string Old_data { get; set; }

public string New_data { get; set; }

public DateTime SearchedFromDate { get; set; }

public DateTime SearchedToDate { get; set; }

public DateTime Changed { get; set; }

public string Username { get; set; }

public string Comment { get; set; }

Contracts.DataContracts.UOSChangeLog

public class UOSChangeLog
{
    public int Id { get; set; }
    public int Accident_nr { get; set; }
    public int Refnr { get; set; }
    public int Action { get; set; }
    public string Old_data { get; set; }
    public string New_data { get; set; }
    public DateTime SearchedFromDate { get; set; }
    public DateTime SearchedToDate { get; set; }
    public DateTime Changed { get; set; }
    public string Username { get; set; }
    public string Comment { get; set; }
}

Here Is how I Insert:

internal static bool SaveUOSChangeLog(List<Contracts.DataContracts.UOSChangeLog> values, string user)
{
    try
    {
        using (var ctx = new StradaDataReviewContext2())
        {
            var newVal = Mapper.Map<List<Contracts.DataContracts.UOSChangeLog>, List<Services.StradaDataReview2Model.UOSChangeLog>>(values);
            foreach(var val in newVal)
            {
                val.Username = user;
                val.Changed = DateTime.Now;
                ctx.UOSChangeLog.Add(val);
            }

            ctx.SaveChanges();
            return true;
        }
    }
}

When I run the code, I get the following error:

InnerException = {"Cannot insert the value NULL into column 'Id', table 'StradaDataReview.dbo.UOSChangeLog'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

How can I make an auto increment of the Id column?

Red
  • 2,728
  • 1
  • 20
  • 22
Bryan
  • 3,421
  • 8
  • 37
  • 77

4 Answers4

3

I guess you are using Code First, which by convention will set the Id property as the primary key in the corresponding table that by default will have been defined as 'Identity'. I think that it is not necessary to set the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute. I would check first the database to see if in the corresponding table the Id column has been created as an Identity column. Also are you defining anything using Fluent API?

V. Oikonomakos
  • 119
  • 1
  • 5
2

This is how you annotate an auto incremented field

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
James Dev
  • 2,979
  • 1
  • 11
  • 16
  • I still get the same error. I don't want to assign my Id-property a value when adding with entity framework. I want the property to automatically be updated. – Bryan Mar 18 '16 at 09:48
  • This will allow the value to be automatically assigned as you asked. – Ric Mar 18 '16 at 09:52
  • Looking at your error message it's strange. The value of Id should be 0 if you are not assigning it as it is an int and not int? Please post your full model class and the full method you are using to save. – James Dev Mar 18 '16 at 09:59
  • Hello, I got the same problem. Does the value of Id will auto-increment when i areadly had a Id of 0 in my database? – Stephane Jul 28 '16 at 10:40
0

Try to Add: [DatabaseGenerated(DatabaseGeneratedOption.None)] Instead of Identity

On your Id field, Like:

[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
Refael
  • 6,753
  • 9
  • 35
  • 54
-3

Your ID is primary key - so nulls are not allowed. It is because your record's ID must be unique.

S. Nadezhnyy
  • 582
  • 2
  • 6