add basic support for entity framework core
This commit is contained in:
@@ -6,4 +6,12 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.13" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.13">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
16
FakePieShop/Models/CategoryRepository.cs
Normal file
16
FakePieShop/Models/CategoryRepository.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
namespace FakePieShop.Models
|
||||||
|
{
|
||||||
|
public class CategoryRepository : ICategoryRepository
|
||||||
|
{
|
||||||
|
private readonly FakePieShopDbContext _fakePieShopDbContext;
|
||||||
|
|
||||||
|
public CategoryRepository(FakePieShopDbContext fakePieShopDbContext)
|
||||||
|
{
|
||||||
|
_fakePieShopDbContext = fakePieShopDbContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Category> AllCategories =>
|
||||||
|
// get all categories in alphabetical order
|
||||||
|
_fakePieShopDbContext.Categories.OrderBy(p => p.CategoryName);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
FakePieShop/Models/FakePieShopDbContext.cs
Normal file
13
FakePieShop/Models/FakePieShopDbContext.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace FakePieShop.Models
|
||||||
|
{
|
||||||
|
public class FakePieShopDbContext : DbContext
|
||||||
|
{
|
||||||
|
// no further configuration needed for this constructor; all is handled by the superclass
|
||||||
|
public FakePieShopDbContext(DbContextOptions<FakePieShopDbContext> options) : base(options) {}
|
||||||
|
|
||||||
|
public DbSet<Category> Categories { get; set; }
|
||||||
|
public DbSet<Pie> Pies { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
35
FakePieShop/Models/PieRepository.cs
Normal file
35
FakePieShop/Models/PieRepository.cs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace FakePieShop.Models
|
||||||
|
{
|
||||||
|
public class PieRepository : IPieRepository
|
||||||
|
{
|
||||||
|
private readonly FakePieShopDbContext _fakePieShopDbContext;
|
||||||
|
|
||||||
|
public PieRepository(FakePieShopDbContext fakePieShopDbContext)
|
||||||
|
{
|
||||||
|
_fakePieShopDbContext = fakePieShopDbContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Pie> AllPies
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _fakePieShopDbContext.Pies.Include(p => p.Category);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Pie> PiesOfTheWeek
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _fakePieShopDbContext.Pies.Include(c => c.Category).Where(p => p.IsPieOfTheWeek);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pie? GetPieById(int pieId)
|
||||||
|
{
|
||||||
|
return _fakePieShopDbContext.Pies.FirstOrDefault(p => p.PieId == pieId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,18 @@
|
|||||||
using FakePieShop.Models;
|
using FakePieShop.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// add services to application
|
// add services to application
|
||||||
|
builder.Services.AddScoped<IPieRepository, PieRepository>();
|
||||||
|
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
|
||||||
|
|
||||||
builder.Services.AddControllersWithViews();
|
builder.Services.AddControllersWithViews();
|
||||||
builder.Services.AddScoped<IPieRepository, MockPieRepository>();
|
builder.Services.AddDbContext<FakePieShopDbContext>(options =>
|
||||||
builder.Services.AddScoped<ICategoryRepository, MockCategoryRepository>();
|
{
|
||||||
|
options.UseSqlServer(
|
||||||
|
builder.Configuration["ConnectionStrings:FakePieShopDbContextConnection"])
|
||||||
|
});
|
||||||
|
|
||||||
WebApplication app = builder.Build();
|
WebApplication app = builder.Build();
|
||||||
|
|
||||||
|
|||||||
@@ -5,5 +5,8 @@
|
|||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*",
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"FakePieShopDbContextConnection": "Server=(localdb)\\mysqllocaldb;\\Database=FakePieShop0123456;Trusted_Connection=true;Multiple_Active_Results=true;"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,16 @@
|
|||||||
"frameworks": {
|
"frameworks": {
|
||||||
"net6.0": {
|
"net6.0": {
|
||||||
"targetAlias": "net6.0",
|
"targetAlias": "net6.0",
|
||||||
|
"dependencies": {
|
||||||
|
"Microsoft.EntityFrameworkCore.SqlServer": {
|
||||||
|
"target": "Package",
|
||||||
|
"version": "[7.0.13, )"
|
||||||
|
},
|
||||||
|
"Microsoft.EntityFrameworkCore.Tools": {
|
||||||
|
"target": "Package",
|
||||||
|
"version": "[7.0.13, )"
|
||||||
|
}
|
||||||
|
},
|
||||||
"imports": [
|
"imports": [
|
||||||
"net461",
|
"net461",
|
||||||
"net462",
|
"net462",
|
||||||
|
|||||||
@@ -13,4 +13,11 @@
|
|||||||
<SourceRoot Include="C:\Users\mikay\.nuget\packages\" />
|
<SourceRoot Include="C:\Users\mikay\.nuget\packages\" />
|
||||||
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
|
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||||
|
<Import Project="$(NuGetPackageRoot)microsoft.entityframeworkcore\7.0.13\buildTransitive\net6.0\Microsoft.EntityFrameworkCore.props" Condition="Exists('$(NuGetPackageRoot)microsoft.entityframeworkcore\7.0.13\buildTransitive\net6.0\Microsoft.EntityFrameworkCore.props')" />
|
||||||
|
<Import Project="$(NuGetPackageRoot)microsoft.entityframeworkcore.design\7.0.13\build\net6.0\Microsoft.EntityFrameworkCore.Design.props" Condition="Exists('$(NuGetPackageRoot)microsoft.entityframeworkcore.design\7.0.13\build\net6.0\Microsoft.EntityFrameworkCore.Design.props')" />
|
||||||
|
</ImportGroup>
|
||||||
|
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||||
|
<PkgMicrosoft_EntityFrameworkCore_Tools Condition=" '$(PkgMicrosoft_EntityFrameworkCore_Tools)' == '' ">C:\Users\mikay\.nuget\packages\microsoft.entityframeworkcore.tools\7.0.13</PkgMicrosoft_EntityFrameworkCore_Tools>
|
||||||
|
</PropertyGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,2 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
|
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||||
|
<Import Project="$(NuGetPackageRoot)system.text.json\7.0.0\buildTransitive\net6.0\System.Text.Json.targets" Condition="Exists('$(NuGetPackageRoot)system.text.json\7.0.0\buildTransitive\net6.0\System.Text.Json.targets')" />
|
||||||
|
<Import Project="$(NuGetPackageRoot)microsoft.extensions.logging.abstractions\7.0.0\buildTransitive\net6.0\Microsoft.Extensions.Logging.Abstractions.targets" Condition="Exists('$(NuGetPackageRoot)microsoft.extensions.logging.abstractions\7.0.0\buildTransitive\net6.0\Microsoft.Extensions.Logging.Abstractions.targets')" />
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user