连接字符串设置假定在
应用设置.json
应用设置.json
{
"ConnectionStrings": {
"UserDatabase": "Server=DESKTOP-2K2A6GN;Database=Assignment4;Trusted_Connection=True;"
}
}
public class UserContext : DbContext {
public UserContext(DbContextOptions<UserContext> options): base(options) {
}
public virtual DbSet<User> tblUser { get; set; }
}
您可以在
Startup
ConfigureServices
方法。
public class Startup {
public Startup(IHostingEnvironment env) {
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
}
static IConfiguration Configuration { get; set; }
public void ConfigureServices(IServiceCollection services) {
services.AddDbContext<UserContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("UserDatabase"));
);
//...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseBlazor<Client.Program>();
}
}