I am new .net core with angular and also . I am using a video tutorial on using this link. In the module 6, Services and Back-End - chapter 7, Creating the Database with EF Core, a database MoviesAPI is created by configuring in appsettings.json and startup.cs. I followed teacher's steps in the video to create a database MoviesAPI in my local SQL Server Express.
First I tried using the teacher's configuration for DefaultConnection in appsettings.json but after using that my command Add-Migration Initial in my package manager console in Visual Studio didn't create any database MoviesAPI in my sql server.
"ConnectionStrings": {
"DefaultConnection": "Data Source=.;Initial Catalog=MoviesAPI;Integrated Security=True;"
}
Then I tried using this below but it didn't still create a database in my sql server on my computer.
"DefaultConnection": "Data Source=LAPTOP-5QTMTD1A\\SQLEXPRESS;Initial Catalog=MoviesAPI;Integrated;Security=True"
In both cases, I got a build succeeded in my package manager console.
My Startup.cs
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using MoviesApi.Filters;
using Microsoft.EntityFrameworkCore;
namespace MoviesApi
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
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.AddDbContext(options=> {
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
services.AddCors(options => {
options.AddDefaultPolicy(builder => {
var frontendURL = Configuration.GetValue("frontend_url");
builder.WithOrigins(frontendURL).AllowAnyMethod().AllowAnyHeader();
});
});
services.AddControllers(options=> {
options.Filters.Add(typeof(MoviesExceptionFilter));
});
//E nsures all actions and controllers have simple caching mechanism in the webapi
services.AddResponseCaching();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "MoviesApi", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,ILogger logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MoviesApi v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
//Intercept http request returned from the cache
//app.UseResponseCaching();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
After reading some comments on similar post on , I ran the the command in my package manager console and it ended up giving me these warnings but I don't know if these warnings have anything to do with not being able to create a databse through Add-Migration Initial command.
Warnings
