C
C#5d ago
morry329#

Unable to create an object of type 'JsonContext'

I have fought this error for about an hour and finally gave up - would like a second set of eyes. I wanted to use dotnet ef migrations add InitialCreate which prompted this error Could you kindly point me in the right direction? https://pastebin.com/GuxUApaW
Pastebin
The full error message:(base) MacBook-Pro-2:CRUDOnepager_Sample ann...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
3 Replies
Jimmacle
Jimmacle5d ago
public JsonContext()
{
Database.EnsureCreated();
}
public JsonContext()
{
Database.EnsureCreated();
}
why do you have this? first of all, you can't call that at that point in the context's lifetime second, EnsureCreated is not compatible with migrations as for the actual error, you need a constructor that accepts a DbContextOptions<JsonContext> and passes it to the base exactly like the message says
'AddDbContext' was called with configuration, but the context type 'JsonContext' only declares a parameterless constructor. This means that the configuration passed to 'AddDbContext' will never be used. If configuration is passed to 'AddDbContext', then 'JsonContext' should declare a constructor that accepts a DbContextOptions<JsonContext> and must pass it to the base constructor for DbContext.
'AddDbContext' was called with configuration, but the context type 'JsonContext' only declares a parameterless constructor. This means that the configuration passed to 'AddDbContext' will never be used. If configuration is passed to 'AddDbContext', then 'JsonContext' should declare a constructor that accepts a DbContextOptions<JsonContext> and must pass it to the base constructor for DbContext.
FusedQyou
FusedQyou4d ago
Also, no need for AddScoped<JsonContext>(); when you already do AddDbContext<JsonContext>()
morry329#
morry329#OP4d ago
So I removed parameterless JsonContext() with Database.EnsureCreated(). Those were replaced with
public JsonContext(DbContextOptions<JsonContext> options)
: base(options)
{
}
public JsonContext(DbContextOptions<JsonContext> options)
: base(options)
{
}
but the error persists (Unable to create an object of type 'JsonContext'. For the different patterns supported at design time, see `https://go.microsoft.com/fwlink/?linkid=851728) No idea what triggerd this error. Do you have any idea?
Search - Microsoft Bing
Ready to let off some steam?
'A Festivus for the rest of us!' This is the unoff

Did you find this page helpful?