Skip to content

Commit 57dcc62

Browse files
dev: EF example
1 parent 1ca08d7 commit 57dcc62

File tree

12 files changed

+90
-17
lines changed

12 files changed

+90
-17
lines changed

.github/workflows/tests.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ jobs:
166166
run: |
167167
cd ./examples/src/DapperExample
168168
dotnet run
169-
- name: YDB Topic example
169+
- name: Run YDB Topic example
170170
run: |
171171
cd ./examples/src/Topic
172172
dotnet run
173+
- name: Run EF example
174+
run: |
175+
cd ./examples/src/EF
176+
dotnet run

examples/src/EF/EF.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
<RootNamespace>EfCore</RootNamespace>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\..\src\EfCore.Ydb\src\EfCore.Ydb.csproj"/>
13+
</ItemGroup>
14+
15+
</Project>

examples/src/EF/Program.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using EfCore.Ydb.Extensions;
3+
4+
await using var db = new BloggingContext();
5+
6+
await db.Database.EnsureCreatedAsync();
7+
8+
Console.WriteLine("Inserting a new blog");
9+
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
10+
await db.SaveChangesAsync();
11+
12+
Console.WriteLine("Querying for a blog");
13+
var blog = await db.Blogs
14+
.OrderBy(b => b.BlogId)
15+
.FirstAsync();
16+
17+
Console.WriteLine("Updating the blog and adding a post");
18+
blog.Url = "https://devblogs.microsoft.com/dotnet";
19+
blog.Posts.Add(new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" });
20+
await db.SaveChangesAsync();
21+
22+
Console.WriteLine("Delete the blog");
23+
db.Remove(blog);
24+
await db.SaveChangesAsync();
25+
26+
internal class BloggingContext : DbContext
27+
{
28+
public DbSet<Blog> Blogs { get; set; }
29+
public DbSet<Post> Posts { get; set; }
30+
31+
protected override void OnConfiguring(DbContextOptionsBuilder options)
32+
=> options.UseYdb("Host=localhost;Port=2136;Database=/local");
33+
}
34+
35+
internal class Blog
36+
{
37+
public int BlogId { get; set; }
38+
public string Url { get; set; }
39+
40+
public List<Post> Posts { get; } = new();
41+
}
42+
43+
internal class Post
44+
{
45+
public int PostId { get; set; }
46+
public string Title { get; set; }
47+
public string Content { get; set; }
48+
49+
public int BlogId { get; set; }
50+
public Blog Blog { get; set; }
51+
}

examples/src/YdbExamples.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YC", "YC\YC.csproj", "{753E
1717
EndProject
1818
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Topic", "Topic\Topic.csproj", "{0FB9A1C2-4B0C-4AE4-9FA2-E0ED37802A6E}"
1919
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EF", "EF\EF.csproj", "{0CE9DF93-1411-4E73-BA88-A66018FAB948}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|Any CPU = Debug|Any CPU
@@ -51,6 +53,10 @@ Global
5153
{0FB9A1C2-4B0C-4AE4-9FA2-E0ED37802A6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
5254
{0FB9A1C2-4B0C-4AE4-9FA2-E0ED37802A6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
5355
{0FB9A1C2-4B0C-4AE4-9FA2-E0ED37802A6E}.Release|Any CPU.Build.0 = Release|Any CPU
56+
{0CE9DF93-1411-4E73-BA88-A66018FAB948}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
57+
{0CE9DF93-1411-4E73-BA88-A66018FAB948}.Debug|Any CPU.Build.0 = Debug|Any CPU
58+
{0CE9DF93-1411-4E73-BA88-A66018FAB948}.Release|Any CPU.ActiveCfg = Release|Any CPU
59+
{0CE9DF93-1411-4E73-BA88-A66018FAB948}.Release|Any CPU.Build.0 = Release|Any CPU
5460
EndGlobalSection
5561
GlobalSection(SolutionProperties) = preSolution
5662
HideSolutionNode = FALSE

src/EfCore.Ydb/src/Extensions/YdbContextOptionsBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace EfCore.Ydb.Extensions;
99

1010
public static class YdbContextOptionsBuilderExtensions
1111
{
12-
public static DbContextOptionsBuilder UseEfYdb(
12+
public static DbContextOptionsBuilder UseYdb(
1313
this DbContextOptionsBuilder optionsBuilder,
1414
string? connectionString,
1515
Action<YdbDbContextOptionsBuilder>? efYdbOptionsAction = null
@@ -24,7 +24,7 @@ public static DbContextOptionsBuilder UseEfYdb(
2424
return optionsBuilder;
2525
}
2626

27-
public static DbContextOptionsBuilder UseEfYdb(
27+
public static DbContextOptionsBuilder UseYdb(
2828
this DbContextOptionsBuilder optionsBuilder,
2929
DbConnection connection,
3030
Action<YdbDbContextOptionsBuilder>? efYdbOptionsAction = null

src/EfCore.Ydb/src/Infrastructure/EntityFrameworkYdbServicesBuilder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ private static readonly IDictionary<Type, ServiceCharacteristics> YdbServices
2020
protected override ServiceCharacteristics GetServiceCharacteristics(Type serviceType)
2121
{
2222
var contains = YdbServices.TryGetValue(serviceType, out var characteristics);
23-
return contains
24-
? characteristics
25-
: base.GetServiceCharacteristics(serviceType);
23+
return contains ? characteristics : base.GetServiceCharacteristics(serviceType);
2624
}
2725
}

src/EfCore.Ydb/src/Infrastructure/Internal/YdbModelValidator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace EfCore.Ydb.Infrastructure.Internal;
44

5-
// TODO: Not required for mvp
65
public class YdbModelValidator(
76
ModelValidatorDependencies dependencies,
87
RelationalModelValidatorDependencies relationalDependencies

src/EfCore.Ydb/src/Infrastructure/Internal/YdbOptionsExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public YdbOptionsExtension()
1313
{
1414
}
1515

16-
protected YdbOptionsExtension(YdbOptionsExtension copyFrom) : base(copyFrom)
16+
private YdbOptionsExtension(YdbOptionsExtension copyFrom) : base(copyFrom)
1717
{
1818
}
1919

src/EfCore.Ydb/src/Query/Internal/YdbQueryTranslationPostprocessorFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace EfCore.Ydb.Query.Internal;
44

5-
public class YdbQueryTranslationPostprocessorFactory(
5+
public sealed class YdbQueryTranslationPostprocessorFactory(
66
QueryTranslationPostprocessorDependencies dependencies,
77
RelationalQueryTranslationPostprocessorDependencies relationalDependencies
88
) : IQueryTranslationPostprocessorFactory
99
{
10-
public virtual QueryTranslationPostprocessor Create(QueryCompilationContext queryCompilationContext)
10+
public QueryTranslationPostprocessor Create(QueryCompilationContext queryCompilationContext)
1111
=> new YdbQueryTranslationPostprocessor(
1212
dependencies,
1313
relationalDependencies,

src/EfCore.Ydb/src/Storage/Internal/YdbRelationalConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class YdbRelationalConnection(RelationalConnectionDependencies dependenci
1414
public IYdbRelationalConnection Clone()
1515
{
1616
var connectionStringBuilder = new YdbConnectionStringBuilder(GetValidatedConnectionString());
17-
var options = new DbContextOptionsBuilder().UseEfYdb(connectionStringBuilder.ToString()).Options;
17+
var options = new DbContextOptionsBuilder().UseYdb(connectionStringBuilder.ToString()).Options;
1818
return new YdbRelationalConnection(Dependencies with { ContextOptions = options });
1919
}
2020
}

0 commit comments

Comments
 (0)