mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
39 lines
960 B
C#
39 lines
960 B
C#
|
|
using Microsoft.EntityFrameworkCore; // DbContext, DbSet<T>
|
|||
|
|
|
|||
|
|
namespace Packt.Shared;
|
|||
|
|
|
|||
|
|
public class Northwind : DbContext
|
|||
|
|
{
|
|||
|
|
public DbSet<Category> Categories { get; set; } = null!;
|
|||
|
|
public DbSet<Product> Products { get; set; } = null!;
|
|||
|
|
|
|||
|
|
protected override void OnConfiguring(
|
|||
|
|
DbContextOptionsBuilder optionsBuilder)
|
|||
|
|
{
|
|||
|
|
string path = Path.Combine(
|
|||
|
|
Environment.CurrentDirectory, "Northwind.db");
|
|||
|
|
|
|||
|
|
optionsBuilder.UseSqlite($"Filename={path}");
|
|||
|
|
|
|||
|
|
/*
|
|||
|
|
string connection = "Data Source=.;" +
|
|||
|
|
"Initial Catalog=Northwind;" +
|
|||
|
|
"Integrated Security=true;" +
|
|||
|
|
"MultipleActiveResultSets=true;";
|
|||
|
|
|
|||
|
|
optionsBuilder.UseSqlServer(connection);
|
|||
|
|
*/
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected override void OnModelCreating(
|
|||
|
|
ModelBuilder modelBuilder)
|
|||
|
|
{
|
|||
|
|
if (Database.ProviderName.Contains("Sqlite"))
|
|||
|
|
{
|
|||
|
|
modelBuilder.Entity<Product>()
|
|||
|
|
.Property(product => product.UnitPrice)
|
|||
|
|
.HasConversion<double>();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|