可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This is probably the biggest waste of time problem I have spent hours on 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();
This gives me an error of
Value cannot be null. Parameter name: source
stacktrace of
[ArgumentNullException: Value cannot be null. Parameter name: source] System.Linq.Enumerable.Any(IEnumerable1 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 the table. The ORM is EF.
回答1:
I had this one a while back, and the answer isn't necessarily what you'd expect. This error message often crops up when your connection string is wrong.
At a guess, you'll need something like this:
What's happening is that it's looking for a data source in the wrong place; Entity Framework specifies it slightly differently. If you post your connection string and EF config then we can check.
回答2:
Somewhere inside the DbContext is a value that is IEnumerable
and is queried with Any()
(or Where()
or Select()
or any other LINQ-method), but this value is null
.
Find out if you put a query together (somewhere outside your example code) where you are using a LINQ-method, or that you used an IEnumerable
as a parameter which is NULL.
回答3:
My reason was different to the rest here so I thought I'd post it for anyone else who might have this issue.
I was calling Count on an instance of DbSet with a filter of null i.e.
dbSet.Count(null);
I found that passing null here was causing the error so I now call the parameter-less method if the filter is null:
if (filter == null) { return dbSet.Count(); } else { return dbSet.Count(filter); }
This sorted the issue for me. This may be an issue for any other methods on DbSet as well.
回答4:
just as an FYI, somebody may find it useful. I was chasing my tail for this error almost 2 days and was always thinking something big and looking for the classes that might be the issue and finally i found it very stupid issue and it was in my mark up (HTML) code in mypage.ascx. the issue was I have a
and this has got a include property and I have some other tables listed here and mistakenly a table was there that has been delete from the database recently and I never noticed and it returning null with other entities. I just removed the stupid table from the include list and I am good to go. hope this can help somebody.
回答5:
Resolved with the following solution
1)Right click on the edmx file, select Open with, XML editor
2)Locate the entity in the edmx:StorageModels element
3) Remove the DefiningQuery entirely
4) Rename the store:Schema="dbo" to Schema="dbo" (if exists)
5)Remove the store:Name property
回答6:
It could be as silly as in my case where savechanges was erroring bcoz the db did not have foreign keys and associations were added to EDM tables. I added foreign keys in the db and regenerated EDM for a fix.
The errors I was seeing are as follows: Case 1 -> when using DBContext for EDM Message=Value cannot be null. Parameter name: source at System.Linq.Enumerable.Any[TSource](IEnumerable1 source, Func
2 predicate)
Case 2 -> when using ObjectContext for EDM Message=Unable to update the EntitySet 'Contact' because it has a DefiningQuery and no element exists in the element to support the current operation.
(Just wanted to throw it in there in case it helps someone).
回答7:
In MVC, View screen is calling method which is in Controller or Repository.cs and assigning return value to any control in CSHTML but that method is actually not implemented in .cs/controller, then CSHTML will throw the NULL parameter exception
回答8:
I got this error when I had an invalid Type for an entity property.
public Type ObjectType {get;set;}
When I removed the property the error stopped occurring.
回答9:
In my case, the problem popped up while configuring the web application on IIS, When the update command on any record was fired this error got generated.
It was a permission issue on App_Data which set to read-only. Right-click the folder, uncheck the Read-only checkbox and you are done. By the way, for testing purpose, I was using the localdb database which was in App_Data folder.