Skip to content

Commit 2aac567

Browse files
Example EF & YC
1 parent 9fc2ef4 commit 2aac567

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

examples/src/EF_YC/EF_YC.csproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="CommandLineParser" Version="2.8.0"/>
12+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0"/>
13+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0"/>
14+
<PackageReference Include="Ydb.Sdk.Yc.Auth" Version="0.2.0"/>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\..\src\EfCore.Ydb\src\EfCore.Ydb.csproj"/>
19+
<ProjectReference Include="..\..\..\src\Ydb.Sdk\src\Ydb.Sdk.csproj"/>
20+
</ItemGroup>
21+
</Project>

examples/src/EF_YC/Program.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using CommandLine;
2+
using EF_YC;
3+
using EfCore.Ydb.Extensions;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.Extensions.Logging;
6+
using Ydb.Sdk.Yc;
7+
8+
await Parser.Default.ParseArguments<CmdOptions>(args).WithParsedAsync(async cmd =>
9+
{
10+
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Information));
11+
var saProvider = new ServiceAccountProvider(saFilePath: cmd.SaFilePath, loggerFactory: loggerFactory);
12+
13+
var options = new DbContextOptionsBuilder<AppDbContext>()
14+
.UseYdb(cmd.ConnectionString, builder =>
15+
{
16+
builder.WithCredentialsProvider(saProvider);
17+
builder.WithServerCertificates(YcCerts.GetYcServerCertificates());
18+
})
19+
.Options;
20+
21+
await using var db = new AppDbContext(options);
22+
db.Database.EnsureCreated();
23+
24+
db.Users.Add(new User { Name = "Alex", Email = "[email protected]" });
25+
db.Users.Add(new User { Name = "Kirill", Email = "[email protected]" });
26+
db.SaveChanges();
27+
28+
var users = db.Users.OrderBy(u => u.Id).ToList();
29+
Console.WriteLine("Users in database:");
30+
foreach (var user in users)
31+
{
32+
Console.WriteLine($"- {user.Id}: {user.Name} ({user.Email})");
33+
}
34+
});
35+
36+
37+
internal class User
38+
{
39+
public int Id { get; set; }
40+
public string Name { get; set; }
41+
public string Email { get; set; }
42+
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
43+
}
44+
45+
internal class AppDbContext(DbContextOptions options) : DbContext(options)
46+
{
47+
public DbSet<User> Users { get; set; }
48+
}

0 commit comments

Comments
 (0)