C
C#8mo ago
Skenderbegu

Entity Framework 6 error - 'Value cannot be null. Parameter name: entitySet'

This piece of code is throwing the above error:
using (var dbContext = new ApplicationDbContext())
{
var x = dbContext.WarningTypes.ToList(); <-- throws entitySet null
...
}
using (var dbContext = new ApplicationDbContext())
{
var x = dbContext.WarningTypes.ToList(); <-- throws entitySet null
...
}
This is my WarningType class:
namespace Domain
{
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;

public class WarningType
{
public Guid Id { get; set; }

[Required]
[StringLength(200)]
public string Name { get; set; }

[Required]
public string Message { get; set; }
}
namespace Domain
{
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;

public class WarningType
{
public Guid Id { get; set; }

[Required]
[StringLength(200)]
public string Name { get; set; }

[Required]
public string Message { get; set; }
}
This is my DbSet in ApplicationDbContext.cs:
public virtual DbSet<WarningType> WarningTypes { get; set; }
public virtual DbSet<WarningType> WarningTypes { get; set; }
I attached the database table picture for WarningTypes.
No description
22 Replies
FusedQyou
FusedQyou8mo ago
I don't use EF without Dependency Injection. Judging by this code the connection string is not passed which is required. Consider using DI in your project rather than making a custom instance each time
Skenderbegu
SkenderbeguOP8mo ago
I already have this on ApplicationDbContext:
public ApplicationDbContext()
: base("name=ApplicationDbContext")
{
}
public ApplicationDbContext()
: base("name=ApplicationDbContext")
{
}
And my webconfig contains
<connectionStrings>
<add name="ApplicationDbContext" connectionString="data source=localhost;initial catalog=DatabaseName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
<connectionStrings>
<add name="ApplicationDbContext" connectionString="data source=localhost;initial catalog=DatabaseName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
Would it not throw an error in the using statement if the connection string was empty or wrong in that case?
Kouhai
Kouhai8mo ago
Can you send the full code for WarningType it seems to be a partial class and the code snippet doesn't include everything
Skenderbegu
SkenderbeguOP8mo ago
That's all actually, it's not a partial class, my mistake
leowest
leowest8mo ago
are u working with legacy app?
Kouhai
Kouhai8mo ago
Hmm, can you attach the full stack trace?
Skenderbegu
SkenderbeguOP8mo ago
Yes, it's a legacy app which was having a few caching issues using some other framework. Now we're changing it to EF6 temporarily until we move to EF core
Skenderbegu
SkenderbeguOP8mo ago
And later on when saving the error log I have the text file below
Skenderbegu
SkenderbeguOP8mo ago
But I don't think the text file is really important because the provided Id at that point is not an actual user
Kouhai
Kouhai8mo ago
I assume you didn't run migration or anything,the db already exists right?
Skenderbegu
SkenderbeguOP8mo ago
Yes, the db already exists
Kouhai
Kouhai8mo ago
What's the table name?
Skenderbegu
SkenderbeguOP8mo ago
WarningTypes I also tried adding the attribute [Table("WarningTypes")] to the WarningType class But still got the same error
Kouhai
Kouhai8mo ago
Hmmm, have you tried specifying the Id as a Key with the attribute Key?
Skenderbegu
SkenderbeguOP8mo ago
Just tried a second time to make sure, but still got the same error
Kouhai
Kouhai8mo ago
Can you change StringLength to MaxLength?
Skenderbegu
SkenderbeguOP8mo ago
Same error still
Kouhai
Kouhai8mo ago
I guess the best bet is to enable ef logging and see what's the generated sql
Skenderbegu
SkenderbeguOP8mo ago
This is all I'm getting:
TEST DEBUG ++++++++++++++++++++++++++++++++++++++++++++
'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-133576509696931889): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data.OracleClient\v4.0_4.0.0.0__b77a5c561934e089\System.Data.OracleClient.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Exception thrown: 'System.ArgumentNullException' in EntityFramework.dll
An exception of type 'System.ArgumentNullException' occurred in EntityFramework.dll but was not handled in user code
Value cannot be null.
TEST DEBUG ++++++++++++++++++++++++++++++++++++++++++++
'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-133576509696931889): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data.OracleClient\v4.0_4.0.0.0__b77a5c561934e089\System.Data.OracleClient.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Exception thrown: 'System.ArgumentNullException' in EntityFramework.dll
An exception of type 'System.ArgumentNullException' occurred in EntityFramework.dll but was not handled in user code
Value cannot be null.
Skenderbegu
SkenderbeguOP8mo ago
No description
Skenderbegu
SkenderbeguOP8mo ago
seems to throw the same error even running a simple query such as:
dbContext.Database.ExecuteSqlCommand("Print 'TEST'");
dbContext.Database.ExecuteSqlCommand("Print 'TEST'");
Kouhai
Kouhai8mo ago
I'm honestly not sure what's causing that, maybe ask in #database They'd be more knowledgable about EF
Want results from more Discord servers?
Add your server