无法解析类型 Microsoft.EntityFrameworkCore.DbContextOptions 的服务
Unable to resolve service for type Microsoft.EntityFrameworkCore.DbContextOptions
当我想使用 ASP.NET 带有视图的核心 MVC 添加控制器时:
enter image description here
这是我的 DbContext
class:
namespace Infrastructure
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
public DbSet<Owner> owners { get; set; }
public DbSet<ProtoFile> protoFiles { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Owner>().Property(x => x.Id).HasDefaultValueSql("NEWID()");
modelBuilder.Entity<ProtoFile>().Property(x => x.Id).HasDefaultValueSql("NEWID()");
modelBuilder.Entity<Owner>().HasData(
new Owner
{
Id = Guid.NewGuid(),
Avatar = "avatar.jpg",
FullName = "Mohammad AlMohammad AlMahmoud",
Profile = ".NET Full Stack Developer"
});
}
}
}
我遇到了这个错误:
enter image description here
您必须将此代码添加到您的程序中class(如果您使用 net 6)
builder.Services.AddDbContext<DataContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
或者如果您使用的是 net 5 或更低版本,请将其添加到启动程序中 class
services.AddDbContext<DataContext>(.....
并删除
base.OnModelCreating(modelBuilder);
I solved this problem when I added this code into Datacontext class
protected override void OnConfiguring(DbContextOptionsBuilder DataContext)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test");
}
and it was solved, but after generation the code i have some problem when i run the application , so i remove it and the app worked successfully
from Microsoft docs https://docs.microsoft.com/en-us/ef/core/dbcontext-configuration/
我终于通过添加 IDesignTimeDbContextFactory
修复了它
public class BloggingContextFactory : IDesignTimeDbContextFactory<BloggingContext>
{
public BloggingContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlite("Data Source=blog.db");
return new BloggingContext(optionsBuilder.Options);
}
}
当我想使用 ASP.NET 带有视图的核心 MVC 添加控制器时:
enter image description here
这是我的 DbContext
class:
namespace Infrastructure
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
public DbSet<Owner> owners { get; set; }
public DbSet<ProtoFile> protoFiles { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Owner>().Property(x => x.Id).HasDefaultValueSql("NEWID()");
modelBuilder.Entity<ProtoFile>().Property(x => x.Id).HasDefaultValueSql("NEWID()");
modelBuilder.Entity<Owner>().HasData(
new Owner
{
Id = Guid.NewGuid(),
Avatar = "avatar.jpg",
FullName = "Mohammad AlMohammad AlMahmoud",
Profile = ".NET Full Stack Developer"
});
}
}
}
我遇到了这个错误:
enter image description here
您必须将此代码添加到您的程序中class(如果您使用 net 6)
builder.Services.AddDbContext<DataContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
或者如果您使用的是 net 5 或更低版本,请将其添加到启动程序中 class
services.AddDbContext<DataContext>(.....
并删除
base.OnModelCreating(modelBuilder);
I solved this problem when I added this code into Datacontext class
protected override void OnConfiguring(DbContextOptionsBuilder DataContext)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test");
}
and it was solved, but after generation the code i have some problem when i run the application , so i remove it and the app worked successfully
from Microsoft docs https://docs.microsoft.com/en-us/ef/core/dbcontext-configuration/
我终于通过添加 IDesignTimeDbContextFactory
修复了它public class BloggingContextFactory : IDesignTimeDbContextFactory<BloggingContext>
{
public BloggingContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlite("Data Source=blog.db");
return new BloggingContext(optionsBuilder.Options);
}
}