1

我有单独的 DAL,需要从中提供 restier 控制器。是否可以使用另一个程序集的 dbcontext ?

我也在尝试它,但是在连接到 dbcontext 的字符串上出现错误。

4

1 回答 1

2

当然这是可能的。您只需在 serviceCollection 中将您的 dbContext 注册为服务。您的连接字符串问题根本与 RESTier 无关。

public static new IServiceCollection ConfigureApi(Type apiType, IServiceCollection services)
    {
        return services.AddScoped<DbContext>(sp => 
          {
            return GetYourInitializedDbContextFromAnywhereYouWant();
          });
    }
于 2017-07-14T07:15:22.667 回答