0

I get the following ArgumentNullException when I insert a rather simple entity into a table. I don't think it matters, but the database is an SQL Server Compact .sdf file.

Value cannot be null. Parameter name: source

at System.Linq.Enumerable.Any[TSource](IEnumerable1 source, Func2 predicate)
at System.Data.Entity.Internal.InternalContext.WrapUpdateException(UpdateException updateException)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
{{ the code below }}

Here is the code that I'm using:

var newMaterial = _localJobs.DbMaterials.Create();

newMaterial.JobID = a_job.ID;
newMaterial.MaterialName = material.Name;
newMaterial.UseType = material.UseType;
newMaterial.Length = material.Length;
newMaterial.Width = material.Width;
newMaterial.Thickness = material.Thickness;
newMaterial.DefaultLength = material.DefaultLength;
newMaterial.DefaultWidth = material.DefaultWidth;
newMaterial.DefaultThickness = material.DefaultThickness;

_localJobs.DbMaterials.Add(newMaterial);
_localJobs.SaveChanges(); // <- The exception occurs here.

I am properly populating every field with valid data. The only key herein is JobID. It is a foreign key GUID with an explicit relation with a table called Job. The proper record already exists in the database.

Here is my table schema.

enter image description here

Jordan
  • 9,642
  • 10
  • 71
  • 141
  • When you debug, do all of the other fields have non-null values? – Corey Adler Nov 18 '13 at 20:15
  • This might help you out: http://stackoverflow.com/questions/3244336/using-linq-to-find-item-in-a-list-but-get-value-cannot-be-null-parameter-name - Might not though. Also: http://stackoverflow.com/questions/16281133/value-cannot-be-null-parameter-name-source – Chris Nov 18 '13 at 20:58
  • What does the Material class look like? – Chris Nov 18 '13 at 21:00
  • Material class is a simple POCO where `Name` is a string; `UseType` is a string; `Length`, `Width`, and `Thickness` are doubles; and `DefaultLength`, `DefaultWidth`, and `DefaultThickness` are doubles. None of these are nullable and all are set to valid values when I debug. – Jordan Nov 19 '13 at 15:22
  • Sorry, I meant DbMaterials. I vaguely remember a similar error if you use fields in your `Context` class, rather than properties. E.g. if you had `public DbSet DbMaterials ;`, you'd want `public DbSet DbMaterials { get; set; }` – Chris Nov 19 '13 at 22:22
  • I am using database first, so my entities are generated. I'm not one to use non-constant public fields anyway. – Jordan Nov 20 '13 at 13:58

3 Answers3

0

This error occurs when you have a field that:

  • Does not allow nulls
  • Does not have a default value
  • That you do not supply a value for in the insert

The reason for the error could be:

  • you are setting one of the fields to null
  • the save changes is also trying to save a row that was added earlier that is not showing up in the code you posted.

Try using SQL Profiler then you will see which insert statement is causing the error.

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • The exception is not to me. It is an exception occuring deep within Entity Framework code that is not caught. The reason for the exception is that `InternalContext.WrapUpdateException` is calling `Enumerable.Any` with a null reference in the `source` parameter. I verified that I am properly setting all of the fields to valid values. When I enter the values manually into the table, everything works just fine. So I don't think the problem is with SQL at all. – Jordan Nov 19 '13 at 14:11
0

Given you will have checked all of the sensible suggestions already provided. Then another possibility is You model and DB dont Match.

Do you have a

Migration outstanding.

.. Double check. I have seen this message before ;-)

phil soady
  • 11,043
  • 5
  • 50
  • 95
0

It's very old thread but still I am going to post answer to it because i had similar issue and I could not find answer on it. Maybe it will help someone.

Answer for me was very silly. My ID column was not an primary key of the table.

Shoter
  • 976
  • 11
  • 23