【发布时间】:2021-04-30 05:43:05
【问题描述】:
我目前确实有以下设置,这些是不同的项目Project.DAL:数据访问层Project.BLL:业务逻辑层Project.Core:接口,模型
@987654324 @:Web API Project.Console:简单的 .NET 控制台应用程序
在Project.DAL 我确实设置了数据库上下文
public class AppDbContext : DbContext
{
public DbSet<Role> Roles { get; set; }
public DbSet<User> Users { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{ }
}
虽然我在 Project.API 中设置了所有依赖注入
services.AddTransient<IUserService, UserService>();
services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("Default"),x => x.MigrationsAssembly("Project.DAL"));
});
当我尝试在Project.Console 中运行Program.cs
class Program
{
private static AppDbContext _appDbContext;
static void Main(string[] args)
{
_appDbContext.Users.ToList();
}
}
未处理的异常。 System.NullReferenceException:对象引用未设置为对象的实例。
有什么想法吗?
提前致谢。
【问题讨论】:
标签: c# .net entity-framework .net-core dependency-injection