.NET MAUI AddDbContext与依赖注入

doinxwow  于 2023-05-08  发布在  .NET
关注(0)|答案(1)|浏览(202)

我在.Net MAUI中有一个应用程序,我想知道是否可以通过依赖注入以某种方式执行services.AddDbContext。我希望能说清楚
我的代码:

public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)
{
    services.AddDbContext<StoreCustomersContext>(opt =>
    {
        connectionString = configurations.GetConnectionString("DefaultConnection");
    });
}

错误:

Severity Code Description Project File Line Status Deleted
     Error CS1061 "IServiceCollection" does not contain a definition for "AddDbContext" or an accessible extension method "AddDbContext" that accepts a first argument of type "IServiceCollection" (are you missing a using directive or an assembly reference?)

谢谢

anauzrmj

anauzrmj1#

如果使用的是SQLServer,则可能缺少包引用,例如Microsoft.EntityFrameworkCore.SqlServer。另外,您需要指定该文件中的名称空间,它应该是Microsoft.Extensions.DependencyInjection

相关问题