mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-06-10 01:28:03 +00:00
Updated readme and added new sample project
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ReadmeSample.Entities;
|
||||
|
||||
namespace ReadmeSample
|
||||
{
|
||||
public class ApplicationDbContext : DbContext
|
||||
{
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<Product> Products { get; set; }
|
||||
public DbSet<Order> Orders { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=ReadmeSample;Trusted_Connection=True");
|
||||
optionsBuilder.UseProjections();
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<OrderItem>().HasKey(x => new { x.OrderId, x.ProductId });
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections;
|
||||
|
||||
namespace ReadmeSample.Entities
|
||||
{
|
||||
public class Order
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int UserId { get; set; }
|
||||
public DateTime CreatedDate { get; set; }
|
||||
|
||||
public decimal TaxRate { get; set; }
|
||||
|
||||
public User User { get; set; }
|
||||
public ICollection<OrderItem> Items { get; set; }
|
||||
|
||||
[Projectable] public decimal Subtotal => Items.Sum(item => item.Product.ListPrice * item.Quantity);
|
||||
[Projectable] public decimal Tax => Subtotal * TaxRate;
|
||||
[Projectable] public decimal GrandTotal => Subtotal + Tax;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace ReadmeSample.Entities
|
||||
{
|
||||
public class OrderItem
|
||||
{
|
||||
public int OrderId { get; set; }
|
||||
public int ProductId { get; set; }
|
||||
public int Quantity { get; set; }
|
||||
|
||||
public Order Order { get; set; }
|
||||
public Product Product { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace ReadmeSample.Entities
|
||||
{
|
||||
public class Product
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public decimal ListPrice { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ReadmeSample.Entities
|
||||
{
|
||||
public class User
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string UserName { get; set; }
|
||||
|
||||
public string EmailAddress { get; set; }
|
||||
|
||||
public ICollection<Order> Orders { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projections;
|
||||
using ReadmeSample.Entities;
|
||||
|
||||
namespace ReadmeSample.Extensions
|
||||
{
|
||||
public static class UserExtensions
|
||||
{
|
||||
[Projectable]
|
||||
public static Order GetMostRecentOrderForUser(this User user, DateTime? cutoffDate)
|
||||
=> user.Orders.Where(x => x.CreatedDate >= cutoffDate).OrderByDescending(x => x.CreatedDate).FirstOrDefault();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ReadmeSample.Entities;
|
||||
using ReadmeSample.Extensions;
|
||||
|
||||
namespace ReadmeSample
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
using var dbContext = new ApplicationDbContext();
|
||||
|
||||
// recreate database
|
||||
dbContext.Database.EnsureDeleted();
|
||||
dbContext.Database.EnsureCreated();
|
||||
|
||||
// Populate with seed data
|
||||
var sampleUser = new User { UserName = "Jon", EmailAddress = "jon@doe.com" };
|
||||
var sampleProduct = new Product { Name = "Blue Pen", ListPrice = 1.5m };
|
||||
var sampleOrder = new Order {
|
||||
User = sampleUser,
|
||||
TaxRate = .19m,
|
||||
CreatedDate = DateTime.UtcNow,
|
||||
Items = new List<OrderItem> {
|
||||
new OrderItem { Product = sampleProduct, Quantity = 5 }
|
||||
}
|
||||
};
|
||||
|
||||
dbContext.AddRange(sampleUser, sampleProduct, sampleOrder);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
// Lets try a query!
|
||||
var query = dbContext.Users
|
||||
.Where(x => x.UserName == "Jon")
|
||||
.Select(x => new {
|
||||
x.GetMostRecentOrderForUser(DateTime.UtcNow.AddDays(-30)).GrandTotal
|
||||
});
|
||||
|
||||
var result = query.First();
|
||||
|
||||
Console.WriteLine($"Jons latest order had a grant total of {result.GrandTotal}");
|
||||
Console.WriteLine($"The following query was used to fetch the results:");
|
||||
Console.WriteLine(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<Nullable>disable</Nullable>
|
||||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
|
||||
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projections.Generator\EntityFrameworkCore.Projections.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
||||
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projections\EntityFrameworkCore.Projections.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user