Visual CSharp .NET - Problema conexion DB (Visual Studio 2022 WebAPI .Net)

 
Vista:
Imágen de perfil de Emanuel

Problema conexion DB (Visual Studio 2022 WebAPI .Net)

Publicado por Emanuel (1 intervención) el 22/06/2023 17:31:04
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Microsoft.EntityFrameworkCore;
 
namespace WebApiAutores
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
 
        public IConfiguration Configuration { get; }
 
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
 
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("defaultConnection")));
 
            services.AddEndpointsApiExplorer();
            services.AddSwaggerGen();
        }
 
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }
 
            app.UseHttpsRedirection();
 
            app.UseRouting();
 
            app.UseAuthorization();
 
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}


Autor.cs
1
2
3
4
5
6
7
8
namespace WebApiAutores.Entidades
{
    public class Autor
    {
        public int Id { get; set; }
        public string Nombre { get; set; }
    }
}


AutoresController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using Microsoft.AspNetCore.Mvc;
using WebApiAutores.Entidades;
 
namespace WebApiAutores.Controllers
{
        [ApiController]
        [Route("api/autores")]
        public class AutoresController: ControllerBase
        {
            [HttpGet]
            public ActionResult<List<Autor>> Get()
            {
                return new List<Autor>() {
                new Autor() { Id = 1, Nombre = "Felipe" },
                new Autor() { Id = 2, Nombre = "Claudia" }
            };
            }
        }
}


ApplicationDbContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
using WebApiAutores.Entidades;
 
namespace WebApiAutores
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions options) : base(options)
        {
        }
 
        public DbSet<Autor> Autores { get; set; }
    }
}


appsetings.Development.json
1
2
3
4
5
6
7
8
9
10
11
12
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "connectionStrings": {
    "defaultConnection": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=CursoWebApis;Integrated Security=True"
  }
}


Este codigo me lanza el siguiente error:

imagen_2023-06-22_122917217
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder