Models represent database tables
class Dish
{
public int Id { get; set; }
[MaxLength(100)]
public string Title { get; set; } = string.Empty;
[MaxLength(1000)]
public string? Notes { get; set; }
public int? Starts { get; set; }
public List<DishIngredient> Ingredients { get; set; } = new();
}
class DishIngredient
{
public int Id { get; set; }
public string Description { get; set; } = string.Empty;
[MaxLength(50)]
public string UnitOfMeasure { get; set; } = string.Empty;
[Column(TypeName = "decimal(5, 2)")]
public decimal Amount { get; set; }
public Dish? Dish { get; set; }
public int DishId { get; set; }
}
Represents connection string with db
class CookbookContext : DbContext
{
public DbSet<Dish> Dishes { get; set; } = null!;
public DbSet<DishIngredient> Ingredients { get; set; } = null!;
public CookbookContext(DbContextOptions<CookbookContext> options) : base(options)
{ }
}
We can use appsettings.json
for the purposes of storing connection data
class CookbookContextFactory : IDesignTimeDbContextFactory<CookbookContext>
{
public CookbookContext CreateDbContext(string[]? args = null)
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
var optionsBuilder = new DbContextOptionsBuilder<CookbookContext>();
optionsBuilder
.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()))
.UseSqlServer(configuration["ConnectionStrings:DefaultConnection"]!);
return new CookbookContext(optionsBuilder.Options);
}
}
appsettings.json
file
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=CookbookLesson;User=sa;Password=***"
}
}
dotnet tool install --global dotnet-ef
Migrations are needed to transfer data from C# to a database table.
From solution directory run dotnet ef migrations add Initial
to add initial migration that will be responsible for converting C# instances to db table records.
To finally create a db run dotnet ef database update
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.Extensions.Configuration.Json
Microsoft.EntityFrameworkCore.Design
Microsoft.Extensions.Logging.Console