我的解决方案中有两个项目。一个是Angular Core 2.2 Web应用程序,另一个是包含域模型和dbcontext的类库项目。 我想将连接字符串传递给dbcontext,以便我可以使用代码优先方法创建数据库。 这是我的连接字符串
class PlatoIngredienteController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$platos = Plato::all();
$ingredientes = Ingrediente::all();
$platoingredientes = PlatoIngrediente::all();
return view('platoingrediente/index', compact('platos','ingredientes','platoingredientes'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$platos = Plato::all();
$ingredientes = Ingrediente::all();
$platoingredientes = PlatoIngrediente::all();
return view('platoingrediente/create', compact('platos','ingredientes','platoingredientes'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'cantidad' => 'required|max:50',
'id_plato' => 'required|max:50'
]);
$platoingrediente = PlatoIngrediente::create($validatedData);
return redirect('/platoingrediente')->with('success','El Plato se guardó correctamente en la base de datos');
}
/**
* Display the specified resource.
*
* @param \App\PlatoIngrediente $platoIngrediente
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\PlatoIngrediente $platoIngrediente
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\PlatoIngrediente $platoIngrediente
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\PlatoIngrediente $platoIngrediente
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
这是ConfigureService方法中的代码
"ConnectionStrings": {
"DataConnection": "Server=.;Database=Test;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=True;"
}
这是我的DBContext
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<TestContext>(context => context.UseSqlServer(Configuration.GetConnectionString("DataConnection")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
我是编码优先方法和点网核心的新手。请指导我该怎么办!
答案 0 :(得分:0)
由于您将连接字符串配置为服务,因此可以像这样简单地注入DbContext
类的构造函数:
public class TestContext : DbContext
{
public TestContext(DbContextOptions<TestContext> options):base(options)
{
}
}
答案 1 :(得分:0)
您必须将IConfiguration
接口作为参数,从Microsoft.Extensions.Configuration
传递到Startup
类的构造函数(将其命名为任何名称,例如,我将其命名为{{1 }}。
然后,替换此行
_configuration
与此:
services.AddDbContextPool<TestContext>(context => context.UseSqlServer(Configuration.GetConnectionString("DataConnection")));
在thar之后,将此构造函数添加到您的services.AddDbContext<TestContext>(options => options.UseSqlServer(_configuration.GetConnectionString("DataConnection")));
类中:
TestContext
使用public TestContext(DbContextOptions<TestContext> options) : base(options)
{
}
中为DBContextOptions
设置的配置为TestContext
类注入种子