mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-03-10 12:56:28 +00:00
Add project files.
This commit is contained in:
19
samples/BasicSample/BasicSample.csproj
Normal file
19
samples/BasicSample/BasicSample.csproj
Normal file
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<Nullable>disable</Nullable>
|
||||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.6" />
|
||||
</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>
|
||||
72
samples/BasicSample/Program.cs
Normal file
72
samples/BasicSample/Program.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using EntityFrameworkCore.Projections;
|
||||
using EntityFrameworkCore.Projections.Extensions;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace BasicSample
|
||||
{
|
||||
public partial class User
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string FirstName { get; set; }
|
||||
|
||||
public string LastName { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public string FullName
|
||||
=> FirstName + " " + LastName;
|
||||
|
||||
[Projectable]
|
||||
public string FullNameFunc()
|
||||
=> FirstName + " " + LastName;
|
||||
}
|
||||
|
||||
public class ApplicationDbContext : DbContext
|
||||
{
|
||||
public ApplicationDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public DbSet<User> Users { get; set; }
|
||||
}
|
||||
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
using var dbConnection = new SqliteConnection("Filename=:memory:");
|
||||
dbConnection.Open();
|
||||
|
||||
using var serviceProvider = new ServiceCollection()
|
||||
.AddDbContext<ApplicationDbContext>(options => {
|
||||
options
|
||||
.UseSqlite(dbConnection)
|
||||
.UseProjections();
|
||||
})
|
||||
.BuildServiceProvider();
|
||||
|
||||
var dbContext = serviceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
dbContext.Database.EnsureCreated();
|
||||
|
||||
var user = new User { FirstName = "Jon", LastName = "Doe" };
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
|
||||
dbContext.SaveChanges();
|
||||
|
||||
var query = dbContext.Users
|
||||
.Select(x => new {
|
||||
Foo = x.FullNameFunc()
|
||||
});
|
||||
|
||||
Console.WriteLine(query.ToQueryString());
|
||||
|
||||
var r1 = query.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user