C
C#6mo ago
IcyIme

problem with entityframework migration

error: An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Failed to load configuration from file 'C:\Users\peter\source\repos\BlazingBlog\BlazingBlog\appsettings.json'. Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[BlazingBlog.Data.BlogContext]' while attempting to activate 'BlazingBlog.Data.BlogContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 code:
using BlazingBlog.Data.Entities;
using Microsoft.EntityFrameworkCore;

namespace BlazingBlog.Data
{
public class BlogContext : DbContext
{
public BlogContext(DbContextOptions<BlogContext> options) : base(options)
{

}

public DbSet<Category> Categories { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<BlogPost> BlogPosts { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
#if DEBUG
optionsBuilder.LogTo(Console.WriteLine);
#endif
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<User>()
.HasData(
new User
{
Id = 1,
Email = "peter.odrok@outlook.sk",
FirstName = "Peter",
LastName = "Odak",
Salt = "dasdasdasd",
Hash = "dsalkdjasklfjdsalkjfdsakljfalskjdas"
}
);
}
}
}
using BlazingBlog.Data.Entities;
using Microsoft.EntityFrameworkCore;

namespace BlazingBlog.Data
{
public class BlogContext : DbContext
{
public BlogContext(DbContextOptions<BlogContext> options) : base(options)
{

}

public DbSet<Category> Categories { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<BlogPost> BlogPosts { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
#if DEBUG
optionsBuilder.LogTo(Console.WriteLine);
#endif
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<User>()
.HasData(
new User
{
Id = 1,
Email = "peter.odrok@outlook.sk",
FirstName = "Peter",
LastName = "Odak",
Salt = "dasdasdasd",
Hash = "dsalkdjasklfjdsalkjfdsakljfalskjdas"
}
);
}
}
}
2 Replies
blueberriesiftheywerecats
U need to fix first error message to make a migration If you have errors in ur project, u cant make migration
kermit
kermit6mo ago
Try the below points to resolve the errors. 1. If the appsettings.json is missing, try creating it. 2. Try checking the format of appsettings.json. 3. Check and Install the missing EntityFrameworkCore nuget packages.