Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Alura.Loja.Testes.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
//GravarUsandoAdoNet();
SaveProduct();
}
private static void SaveProduct()
{
Product product = new Product();
product.Name = "Harry Potter e a Ordem da Fênix";
product.Category = "Livros";
product.Price = 19.89;
using (var context = new StoreContext())
{
context.Products.Add(product);
context.SaveChanges();
}
}
private static void GravarUsandoAdoNet()
{
Product product = new Product();
product.Name = "Harry Potter e a Ordem da Fênix";
product.Category = "Livros";
product.Price = 19.89;
using (var repo = new ProductDAO())
{
repo.Adicionar(product);
}
}
}
}
Loja Contexto
using Microsoft.EntityFrameworkCore;
using System;
namespace Alura.Loja.Testes.ConsoleApp
{
public class StoreContext : DbContext
{
public DbSet<Product> Products {get; set;}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.
UseSqlServer(@"aqui a connection string");
}
}
}
Tabela
CREATE TABLE [dbo].[Products] (
[Id] INT IDENTITY(1,1) NOT NULL,
[Name] NVARCHAR(MAX),
[Category] NVARCHAR(MAX),
[Price] FLOAT (53),
CONSTRAINT [PK_Products] PRIMARY KEY ([Id])
);