Skip to content

Instantly share code, notes, and snippets.

@Whistler092
Created May 8, 2021 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Whistler092/b137f6d343c15d72fb8b156bb9432fe4 to your computer and use it in GitHub Desktop.
Save Whistler092/b137f6d343c15d72fb8b156bb9432fe4 to your computer and use it in GitHub Desktop.
Add EF with SqlServer

Steps for mac

Run SQL Server With Docker

https://www.cloudiqtech.com/install-run-sql-server-docker-container-mac/

$ docker pull microsoft/mssql-server-linux:2017-latest

$ docker run -d --name macsqlserver -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Passw1rd' -e 'MSSQL_PID=Developer' -p 1433:1433 microsoft/mssql-server-linux:2017-latest

With Visual Studio Code, you can connect to MSSQL

https://marketplace.visualstudio.com/items?itemName=ms-mssql.mssql

Run migrations,

https://docs.microsoft.com/en-gb/ef/core/cli/dotnet

(In the project path)

% dotnet ef migrations add InitialCreate
% dotnet ef database update
using System;
using Microsoft.EntityFrameworkCore;
using peliculasAPI.Entidades;
namespace API
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<Genero> Generos { get; set; }
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"frontend_url": "http://localhost:3000",
"connectionStrings": {
"defaultConnection": "Data Source=.;Initial Catalog=PeliculasAPI;User ID=sa;Password=********;"
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using peliculasAPI.Entidades.Validaciones;
namespace API.Entidades
{
public class Genero
{
public int Id { get; set; }
[Required(ErrorMessage = "El campo {0} es requerido")]
[StringLength(maximumLength: 50)]
public string Nombre { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using peliculasAPI.Entidades;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace API.Controllers
{
[Route("api/generos")]
[ApiController]
public class GenerosController : ControllerBase
{
private readonly ILogger<GenerosController> logger;
private readonly ApplicationDbContext context;
public GenerosController(ILogger<GenerosController> logger, ApplicationDbContext context)
{
this.logger = logger;
this.context = context;
}
[HttpGet]
public async Task<ActionResult<List<Genero>>> Get()
{
return await context.Generos.ToListAsync();
}
[HttpPost]
public async Task<ActionResult> Post([FromBody] Genero genero)
{
context.Add(genero);
await context.SaveChangesAsync();
return NoContent();
}
}
}
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer(Configuration.GetConnectionString("defaultConnection"))
);
//...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment