I've started out using the CodeFirst approach in the entity framework.
When running my code I am unable to perform any operation on the DbSets within the DbContext - ProductsDb
I have checked the connection string and I think it is correct but attempting to perform operation results in the error
Value cannot be null, parameter source.
Here is the ProductsDb class
public class ProductsDb : DbContext
{
public ProductsDb(string connectionString) : base(connectionString)
{
}
public ProductsDb()
{
}
public DbSet<AProduct> AProducts;
public DbSet<BProduct> BProducts;
public DbSet<CProduct> CProducts;
public DbSet<DProduct> DProducts;
}
The connection string is defined in the app.config as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="ProductsDb" providerName="System.Data.SqlClient"
connectionString="Data Source=MyPC\DEV;Initial Catalog=Test;User ID=sa;
Password=pass;" />
</connectionStrings>
</configuration>
The code that throws the error is:
GetAProduct(string id)
{
AProduct aProduct = null;
using (ProductsDb productsDb = new ProductsDb())
{
aProduct = (from a in productsDb.AProducts
where a.Id== id
select a).FirstOrDefault();
}
return aProduct;
}
All of the product classes are plain old C# classes.
Any help would be really appreciated, I'm starting to pull my hair out. Never have any problems when writing Sql queries.
UPDATE: Copy Paste error the GetAProduct method has been altered.
Value cannot be null. Parameter name: InString: This error indicates that an item failed to migrate because it has corrupt MAPI properties. More specifically, Exchange Web Services (EWS) returned a MAPI property whose value should always be a non-empty string, but was returned as an empty string.
The steps to update an existing entity are quite simple. First retrieve an instance of the entity from the EntitySet<T> (in our case ObjectSet<Customer>), then edit the properties of the Entity and finally call SaveChanges() on the context.
Parameter name: entitySet - Stack Overflow Value cannot be null. Parameter name: entitySet Bookmark this question. Show activity on this post. public class Project { public int ProjectId { get; set; } public string Name { get; set; } public int?
Because Entity Framework looks for properties. If you will explore sources of DbContext class, you will see, that in constructor in searches for declared set properties and initializes them:
(Parameter 'connectionString') [Solved] + Value cannot be null. (Parameter 'connectionString') Usually during application development (asp.net core, Web Api, blazor, Entity Framework, Azure and .net core) issues arise, and challenges the developer.
Value cannot be null. Parameter name: extent I'm using EF6 code first to create my db. Everything was working well last night, now when i run update-database command, I get the following exception: PM> update-database Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
You should define properties instead of fields for sets in your context class. So, instead of fields
public DbSet<AProduct> AProducts;
You should use properties
public DbSet<AProduct> AProducts { get; set; }
Because Entity Framework looks for properties. If you will explore sources of DbContext
class, you will see, that in constructor in searches for declared set properties and initializes them:
private void DiscoverAndInitializeSets()
{
new DbSetDiscoveryService(this).InitializeSets();
}
This set discover service looks only for properties:
var flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
var properties =
from p in this._context.GetType().GetProperties(flags)
where (p.GetIndexParameters().Length == 0) &&
(p.DeclaringType != typeof(DbContext))
select p);
foreach(PropertyInfo info in properties)
{
// ...
}
If you will declare simple field of DbSet<T>
type, EF will skip it's initialization, and field will have value null
after context is created. Thats why where enumerator was throwing null source
exception here:
productsDb.AProducts.Where(a => a.Id== id)
I had the same error message though the cause was different:
I'd made all of the POCOs (and their properties) that were being used by the DbContext internal
instead of public
. As Sergey Berezovskiy has demonstrated in his answer, EF requires all class members to be public properties to initalise them. Changing them to public
fixed the error straight away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With