Add project files.

This commit is contained in:
Koen Bekkenutte
2021-05-27 04:17:35 +08:00
commit 1c83f3d6ad
48 changed files with 2554 additions and 0 deletions

View 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>

View 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();
}
}
}