The value cannot be null. Parameter Name: Source

This is perhaps the biggest waste of time, which I spent a lot of time solving for a long time.

var db = new hublisherEntities(); establishment_brands est = new establishment_brands(); est.brand_id = 1; est.establishment_id = 1; est.price = collection["price"]; est.size = collection["size"]; db.establishment_brands.Add(est); db.SaveChanges(); 

It gives me an error

The value cannot be null. Parameter Name: Source

stacktrace

[ArgumentNullException: value cannot be null. Parameter Name: source] System.Linq.Enumerable.Any (IEnumerable 1 source, Func 2 predicate) +4083335 System.Data.Entity.Internal.InternalContext.WrapUpdateException (UpdateException updateException) +87
System.Data.Entity.Internal.InternalContext.SaveChanges () +193
System.Data.Entity.Internal.LazyInternalContext.SaveChanges () +33
System.Data.Entity.DbContext.SaveChanges () +20 ......

I just want to add an entity to a table. ORM is EF.

+94
c # asp.net-mvc entity-framework
Apr 29 '13 at 14:24
source share
15 answers

I had this a while ago, and the answer is not necessarily what you expect. This error message often occurs when the connection string is incorrect.

I suppose you will need something like this:

 <connectionStrings> <add name="hublisherEntities" connectionString="Data Source=localhost;Initial Catalog=hublisher;Integrated Security=True;" providerName="System.Data.SqlClient" /> </connectionStrings> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="Data Source=localhost;Initial Catalog=hublisher;Integrated Security=True" /> </parameters> </defaultConnectionFactory> </entityFramework> 

What happens is that he is looking for a data source in the wrong place; Entity Framework defines it a little differently. If you post your connection string and EF configuration, we can check.

+34
Apr 29 '13 at 14:43
source share

Somewhere inside the DbContext there is an IEnumerable value and is requested using Any() (or Where() or Select() or any other LINQ method), but this value is null .

Find out if you put the query together (somewhere outside your sample code), where you use the LINQ method, or that you used IEnumerable as a NULL parameter.

+135
Apr 29 '13 at 14:32
source share

My reason was different from the rest here, so I decided to publish it for anyone who might have this problem.

I called Count on a DbSet instance with a null ie filter

 dbSet.Count(null); 

I found that passing null here caused an error, so now I am calling a method without a parameter if the filter is null:

  if (filter == null) { return dbSet.Count(); } else { return dbSet.Count(filter); } 

This sorted the problem out for me. This can be a problem for any other methods on DbSet.

+10
Aug 11 '14 at 1:35
source share

like FYI, someone might find this helpful. I chased my tail for this error for almost 2 days and always thought about something big and looked for classes that might be the problem, and finally I found this a very silly problem, and that was in my meaning (HTML) code on mypage .ascx, the problem was that I have <asp:EntityDataSource> , and I have an include property, and I have some other tables listed here, and mistakenly there was a table that was recently deleted from the database, and I never noticed it and returned null with other objects. I just deleted the dumb table from the inclusion list, and I'm good to go. hope this can help someone.

+3
Oct 11 '13 at 13:45
source share

Solved using the following solution

1) Right-click on the edmx file, select Open with, XML editor

2) Find the object in the edmx element: StorageModels

3) Completely remove DefiningQuery

4) Rename the repository: Schema = "dbo" to Schema = "dbo" (if exists)

5) Delete repository: property name

+1
Dec 30 '15 at 2:49
source share

In case someone else is here with my problem installing DB First Entity Framework.

In short, I needed to overload the Entities constructor to accept the connection string, the reason is that I could use the Asp.Net Core dependency injection container, retrieving the connection string from appsettings.json, rather than magically getting it from App.config. file when calling the constructor without parameters.

I forgot to add calls to initialize my DbSets in a new overload. Thus, an automatically generated constructor without parameters looked something like this:

  public MyEntities() : base("name=MyEntity") { Set1 = Set<MyDbSet1>(); Set2 = Set<MyDbSet2>(); } 

And my new overload looked like this:

  public MyEntities(string connectionString) : base(connectionString) { } 

The solution was to add those initializers that the automatically generated code takes care of, a simple skipped step:

  public MyEntities(string connectionString) : base(connectionString) { Set1 = Set<MyDbSet1>(); Set2 = Set<MyDbSet2>(); } 

This really led me to a loop because some of the calls in our Respository that used DbContext worked fine (those that did not need these initialized DBSets), while others threw the runtime error described in the OP.

+1
May 30 '18 at 16:56
source share

Make sure you embed the repository in the service designer. This solved it for me. :: smacking forehead ::

+1
Aug 09 '18 at 15:39
source share

It can be as stupid as in my case when savechanges was a bcoz error. Db did not have foreign keys, and associations were added to the EDM tables. I added foreign keys in db and regenerated EDM for fix.

The errors that I saw are the following: Case 1 -> when using DBContext for EDM Message = Value cannot be null. Parameter name: source in System.Linq.Enumerable.Any [TSource] (predicate IEnumerable 1 source, Func 2)

Case 2 → when using ObjectContext for EDM Message = Unable to update EntitySet "Contact" because it has DefiningQuery and there is no element in the element to support the current operation.

(Just wanted to drop him there if that helps someone).

0
Oct 24 '13 at 14:18
source share

In MVC, the View screen calls a method that is located in Controller or Repository.cs and assigns a return value to any control in CSHTML, but this method is not actually implemented in .cs / controller, then CSHTML will throw a NULL parameter exception

0
Jan 15 '14 at 21:06
source share

I got this error when I had an invalid type for entity property.

 public Type ObjectType {get;set;} 

When I deleted the property, an error occurred.

0
Dec 11 '16 at 15:11
source share

In my case, a problem occurred while setting up a web application in IIS. When the update command in any record was run, this error occurred.

This was a permissions issue on App_Data, which is set to read-only. Right-click the folder, uncheck the "Read-Only" box, and you're done. By the way, for testing purposes, I used the localdb database, which was located in the App_Data folder.

0
Jun 23 '17 at 10:33
source share

My mistake is to forget to add .ThenInclude (s => s.SubChildEntities) to the parent .Include (c => c.SubChildEntities) to the Controller action when trying to call SubChildEntities in the Razor view.

 var <parent> = await _context.Parent .Include(c => c.<ChildEntities>) .ThenInclude(s => s.<SubChildEntities>) .SingleOrDefaultAsync(m => m.Id == id); 

It should be noted that the IntelliSense community for Visual Studio 2017 does not accept the SubChildEntities object in the lambda expression in .ThenInclude (). However, it successfully compiles and runs.

0
Aug 29 '18 at 12:51
source share

I know this is far from the 2013 question, but this symptom may occur if you do not have lazy loading enabled when porting an ASP.NET 5 application to ASP.NET Core, and then when trying to upgrade to Entity Framework Core 2.x (from EF 6). In Entity Framework Core, delayed download proxy support has been moved to a separate package , so you must install it.

This is especially true if all you have downloaded is the Entity Framework Core Sql Server package (which includes the Entity Framework perfectly).

After installing the proxy package, as they say in the docs, call .UseLazyLoadingProxies() in the DbContext parameter constructor (in the Startup DI settings section or wherever you configure your DbContext), and the navigation property that caused the above exception will stop throwing it. and will work as before.

0
Mar 14 '19 at 3:25
source share

Take a row in the database and make all columns empty in that row, for example, "NULL". Now pass this NULL value using try catch or if else.

0
Apr 01 '19 at 21:41
source share

I got exactly this error in the .Net Core 2.2 Entity Framework because I did not have set; in my DbContext like this:

 public DbSet<Account> Account { get; } 

changed to:

 public DbSet<Account> Account { get; set;} 

However, this did not throw an exception until I tried to use the linq query with Where() and Select() as others mentioned above.

I tried to install DbSet read-only. I will continue to try ...

0
Apr 13 '19 at 21:22
source share



All Articles