0

I am using EntityFramework.BulkInsert from

https://efbulkinsert.codeplex.com/

trying to insert a number of records from a List into the database.

What I am getting is an exception about an int field where values that equals DBNull.Value are not allowed.

However, it is not a nullable field in the model, so there is at least a value of 0 in it. The concept of setting that value to null is not applicable. In fact I am setting the value explicitely. I have tried other values than 0, getting the same exception.

Why do I get the exception?

Details

I have a list with entities like this:

var foos = new List<Foo>();

where Foo is an entity class defined as:

public partial class Foo
{
  public int id { get; set; }
  public int SendAttempts { get; set; }
  public Nullable<System.DateTime> LastSendAttempt { get; set; }
  public string LastSendAttemptMessage { get; set; }
  public Nullable<System.DateTime> SendDate { get; set; }
  public int BarID { get; set; }
  public virtual Bar Bar { get; set; }
}

The list is populated with a loop that adds entities like this:

using (var transactionScope = new TransactionScope())
{
    var foos = new List<Foo>();
    foreach (var id in ids)
    {
      var foo = new Foo
      {
        SendAttempts = 0,
        SendDate = DateTime.Now,
        BarID = id
      };

      foos.Add(foo);
    }

  _entities.BulkInsert(foos);

  _entities.SaveChanges();
  transactionScope.Complete();
}

and the exception occurs at the BulkInsert line with the message

Column 'SendAttempts' does not allow DBNull.Value.

Community
  • 1
  • 1
Anders Lindén
  • 6,839
  • 11
  • 56
  • 109

1 Answers1

-2

I had same problem. Check column order in EF model and Database. Must be same.