Reputation: 815
I'm trying to play with Entity Framework Core. So, during the course. I scafolded my existing DataBase Objects. So, application generated the EmployeeContext.cs file with all the DBSet objects. And Upon that it generated the OnConfiguring() overloaded method.
And now I added the below code in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthorization(options =>
{
//options.AddPolicy("RequireManagerOnly", policy =>
// policy.RequireRole("Manager"));
options.AddPolicy(
"ApiUser", policy =>
policy.Requirements.Add(
new ApiUserVerification(5)));
});
services.AddSingleton<IAuthorizationHandler,
ApiRoleHandler>();
var connectionStrings = Configuration.GetConnectionString("DefaultConnections");
services.AddDbContext<EmployeeContext>(options => options.UseSqlServer(connectionStrings));
}
EmployeeContext.cs
public EmployeeContext()
{
}
public EmployeeContext(DbContextOptions<EmployeeContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer("server details");
}
}
But now, when I commented the OnConfiguring() overloaded method in the context class, and posting a get request from POSTMAN, I'm getting 500-Internal Server Error. If I uncomment the OnConfiguring(), I'm able to post the request. I don't understand why? Though I registered the DbContext in Startup.cs
Could anyone provide where I'm missing.
Thank you
Upvotes: 1
Views: 2871
Reputation: 1767
Hi you can read more about it here: https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-strings
In short if your working with Universal Windows Platform (UWP), you would use the:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
to setup your DBContext.
But since your using AspNet Core, it is good practice to have all setup in the Startup.cs
public void ConfigureServices(IServiceCollection services)
if you don't delete this code:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer("server details");
}
}
it will override the setup you did in startup.cs and use "optionsBuilder.UseSqlServer("server details");" as your connection string.
You might have done a typo in your startup aswell.
services.AddDbContext<Employee>(options => options.UseSqlServer(connectionStrings));
should perhaps be:
services.AddDbContext<EmployeeContext>(options => options.UseSqlServer(connectionStrings));
Upvotes: 2