C
C#5mo 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
FusedQyou5mo 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
Skenderbegu5mo 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
Kouhai5mo 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
Skenderbegu5mo ago
That's all actually, it's not a partial class, my mistake
leowest
leowest5mo ago
are u working with legacy app?
Kouhai
Kouhai5mo ago
Hmm, can you attach the full stack trace?
Skenderbegu
Skenderbegu5mo 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
Skenderbegu5mo ago
And later on when saving the error log I have the text file below
Skenderbegu
Skenderbegu5mo 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
Kouhai5mo ago
I assume you didn't run migration or anything,the db already exists right?
Skenderbegu
Skenderbegu5mo ago
Yes, the db already exists
Kouhai
Kouhai5mo ago
What's the table name?
Skenderbegu
Skenderbegu5mo ago
WarningTypes I also tried adding the attribute [Table("WarningTypes")] to the WarningType class But still got the same error
Kouhai
Kouhai5mo ago
Hmmm, have you tried specifying the Id as a Key with the attribute Key?
Skenderbegu
Skenderbegu5mo ago
Just tried a second time to make sure, but still got the same error
Want results from more Discord servers?
Add your server