namespace sqlserver_connection_demo
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
private SqlConnectionStringBuilder stringBuilder = new SqlConnectionStringBuilder()
{
InitialCatalog = "DemoDB",
DataSource = "localhost",
UserID = "sa",
Password = "sa"
};
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<DemoContext>(opt =>
{
opt.UseSqlServer(stringBuilder.ToString())
})
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
Source収録記事
この snippet は記事の「フェーズ1:プロジェクト側 / Start.csを修正する」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。
同じ記事から
4 件namespace sqlserver_connection_demo.Models { public class User {未収録
Modelを作る
#89e3af841026
using Microsoft.EntityFrameworkCore; namespace sqlserver_connection_demo.Models {未収録
Contextを作る
#79dce8b7ef74
private SqlConnectionStringBuilder stringBuilder = new SqlConnectionStringBuilder() { InitialCatalog = "DemoDB", DataSource = "localhost",未収録
Start.csを修正する
#8e195404d5da
services.AddDbContext<DemoContext>(opt => { opt.UseSqlServer(stringBuilder.ToString()); });未収録
Start.csを修正する
#16ca3ab0fab2
