Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ jobs:
strategy:
fail-fast: false
matrix:
source-dir: ["./src/", "./examples/src/", "./slo/src/"]
source-dir: ["./src/", "./examples/", "./slo/src/"]
include:
- source-dir: "./src/"
solutionFile: "YdbSdk.sln"
- source-dir: "./examples/src/"
- source-dir: "./examples/"
solutionFile: "YdbExamples.sln"
- source-dir: "./slo/src/"
solutionFile: "src.sln"
solutionFile: "src.sln"
name: autoformat check
runs-on: ubuntu-latest
steps:
Expand All @@ -41,7 +41,7 @@ jobs:
strategy:
fail-fast: false
matrix:
solutionPath: ["./src/YdbSdk.sln", "./examples/src/YdbExamples.sln", "./slo/src/src.sln"]
solutionPath: ["./src/YdbSdk.sln", "./examples/YdbExamples.sln", "./slo/src/src.sln"]
runs-on: ubuntu-latest
name: Inspection
steps:
Expand Down
24 changes: 16 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,32 @@ jobs:
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ matrix.dotnet-version }}
- name: Run ADO.NET example
- name: Run Ydb.Sdk.AdoNet.QuickStart
run: |
docker cp ydb-local:/ydb_certs/ca.pem ~/
cd ./examples/src/AdoNet
cd ./examples/Ydb.Sdk.AdoNet.QuickStart
dotnet run
- name: Run Dapper example
- name: Run Ydb.Sdk.AdoNet.Dapper.QuickStart
run: |
cd ./examples/src/DapperExample
cd ./examples/Ydb.Sdk.AdoNet.Dapper.QuickStart
dotnet run
- name: Run YDB Topic example
- name: Run Ydb.Sdk.Topic.QuickStart
run: |
cd ./examples/src/Topic
cd ./examples/Ydb.Sdk.Topic.QuickStart
dotnet run
- name: Run Entity Framework Core example
- name: Run EntityFrameworkCore.Ydb.QuickStart
run: |
cd ./examples/src/EntityFrameworkCore.Ydb.QuickStart
cd ./examples/EntityFrameworkCore.Ydb.QuickStart
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet ef migrations add InitialCreate
dotnet ef database update
dotnet run
- name: Run EntityFrameworkCore.Ydb.Samples/Database.Operations.Tutorial
run: |
cd ./examples/EntityFrameworkCore.Ydb.Samples/Database.Operations.Tutorial
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet ef migrations add InitialCreate
dotnet ef database update
dotnet run
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>EntityFrameworkCore.Ydb</RootNamespace>
<RootNamespace>EntityFrameworkCore.Ydb.QuickStart</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\EFCore.Ydb\src\EntityFrameworkCore.Ydb.csproj" />
<ProjectReference Include="..\..\src\EFCore.Ydb\src\EntityFrameworkCore.Ydb.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Entity Framework Core YDB Quick Start

A sample application from the [official documentation](https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli)
A sample application from
the [official documentation](https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli)
shows how to get started with EF, define a model, populate it with data and then query the database.

## Running QuickStart

1. Setup [YDB local](https://ydb.tech/docs/en/reference/docker/start).

2. Create the database:
```bash
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet ef migrations add InitialCreate
dotnet ef database update
```

3. Run the app: `dotnet run`
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Section_1.HR</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\EFCore.Ydb\src\EntityFrameworkCore.Ydb.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// using Microsoft.EntityFrameworkCore;

using EntityFrameworkCore.Ydb.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;

namespace Section_1.HR;

public class HRContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }

public HRContext()
{
}

public HRContext(DbContextOptions<HRContext> options) : base(options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder
.UseYdb("Host=localhost;Port=2136;Database=/local")
.LogTo(Console.WriteLine, new[] { DbLoggerCategory.Database.Command.Name }, LogLevel.Information);
}

internal class HRContextContextFactory : IDesignTimeDbContextFactory<HRContext>
{
public HRContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<HRContext>();

// IMPORTANT!
// Disables retries for the migrations context.
// Required because migration operations may use suppressed or explicit transactions,
// and enabling retries in this case leads to runtime errors with this provider.
//
// "System.NotSupportedException: User transaction is not supported with a TransactionSuppressed migrations or a retrying execution strategy."
//
// Bottom line: ALWAYS disable retries for design-time/migration contexts to avoid migration failures and errors.
return new HRContext(
optionsBuilder
.UseYdb("Host=localhost;Port=2136;Database=/local", builder => builder.DisableRetryOnFailure())
.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.PendingModelChangesWarning))
.Options
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Section_1.HR;

public class Department
{
public int Id { get; set; }
public required string Name { get; set; }
}

public class Employee
{
public int Id { get; set; }
public required string FirstName { get; set; }
public required string LastName { get; set; }
public required DateTime JoinedDate { get; set; }
public required decimal Salary { get; set; }
public Department? Department { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// See https://aka.ms/new-console-template for more information

using Section_1.HR;

InsertDepartments();
SelectDepartments();

return;


static void InsertDepartments()
{
var departments = new List<Department>
{
new() { Name = "Sales" },
new() { Name = "Marketing" },
new() { Name = "Logistics" }
};

using var context = new HRContext();

foreach (var department in departments)
{
context.Departments.Add(department);
}

context.SaveChanges();
}

static void SelectDepartments()
{
using var context = new HRContext();
var departments = context.Departments.ToList();

foreach (var department in departments)
{
Console.WriteLine($"{department.Id} - {department.Name}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Entity Framework Core YDB Add Entity Sample

This sample introduces the basics of using EF Core with YDB.
It covers what EF Core is, how to create a simple .NET app with EF Core, how to define a data model, set up a database connection, and perform basic CRUD operations via the EF Core API.

Based on the [tutorial](https://www.csharptutorial.net/entity-framework-core-tutorial/getting-started-with-entity-framework-core/).

## Running QuickStart

1. Set up [YDB local](https://ydb.tech/docs/en/reference/docker/start).

2. Install the EF Core CLI tool and dependencies (if needed):
```bash
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
```

3. Create the database and apply migrations:
```bash
dotnet ef migrations add InitialCreate
dotnet ef database update
```

4. Run the sample:
```bash
dotnet run
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Database.Operations.Tutorial</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\EFCore.Ydb\src\EntityFrameworkCore.Ydb.csproj" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Database.Operations.Tutorial;

public class Department
{
public int Id { get; set; }
public required string Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Database.Operations.Tutorial;

public class Employee
{
public int Id { get; set; }
public required string FirstName { get; set; }
public required string LastName { get; set; }
public required decimal Salary { get; set; }
public required DateTime JoinedDate { get; set; }

// Foreign key property to the Department
public int DepartmentId { get; set; }

// Reference navigation to Department
public Department Department { get; set; } = null!;

// Reference navigation to EmployeeProfile
public EmployeeProfile? Profile { get; set; }

// collection navigation to Employee
public List<Skill> Skills { get; set; } = new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Database.Operations.Tutorial;

public class EmployeeProfile
{
public int Id { get; set; }
public required string Phone { get; set; }
public required string Email { get; set; }

public int EmployeeId { get; set; }
public Employee Employee { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using EntityFrameworkCore.Ydb.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Database.Operations.Tutorial;

public class HRContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<EmployeeProfile> EmployeeProfiles { get; set; }
public DbSet<Skill> Skills { get; set; }

public HRContext()
{
}

public HRContext(DbContextOptions<HRContext> options) : base(options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();

var connectionString = configuration.GetConnectionString("Local");

optionsBuilder.UseYdb(connectionString)
.LogTo(Console.WriteLine, new[] { DbLoggerCategory.Database.Command.Name }, LogLevel.Information)
.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.PendingModelChangesWarning))
.EnableSensitiveDataLogging();
}
}
Loading
Loading