Skip to content

Instantly share code, notes, and snippets.

@kevinah95
Last active November 7, 2024 01:25
Show Gist options
  • Save kevinah95/f4478225e7c56996967877c317ca22a1 to your computer and use it in GitHub Desktop.
Save kevinah95/f4478225e7c56996967877c317ca22a1 to your computer and use it in GitHub Desktop.
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Tools
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace APIConBD.Entities;
[Table("Todos")]
public partial class Todo
{
[Key]
public int Id { get; set; }
public string? Name { get; set; }
public int? IsCompleted { get; set; }
}
using System;
using System.Collections.Generic;
using APIConBD.Entities;
using Microsoft.EntityFrameworkCore;
namespace APIConBD.Context;
public partial class TodoDbContext : DbContext
{
public TodoDbContext()
{
}
public TodoDbContext(DbContextOptions<TodoDbContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseSqlite("Data Source=identifier.sqlite");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Todo>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.IsCompleted).HasDefaultValue(0);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment