Skip to content

Commit a671e48

Browse files
authored
Make ParameterEndToEnd use SQL Server. (#2061)
* Make ParameterEndToEnd use SQL Server.
1 parent a0e3473 commit a671e48

File tree

4 files changed

+28
-33
lines changed

4 files changed

+28
-33
lines changed

playground/ParameterEndToEnd/ParameterEndToEnd.ApiService/ParameterEndToEnd.ApiService.csproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7-
<InvariantGlobalization>true</InvariantGlobalization>
87
</PropertyGroup>
98

109
<ItemGroup>
11-
<ProjectReference Include="..\..\..\src\Components\Aspire.Microsoft.Azure.Cosmos\Aspire.Microsoft.Azure.Cosmos.csproj" />
10+
<ProjectReference Include="..\..\..\src\Components\Aspire.Microsoft.EntityFrameworkCore.SqlServer\Aspire.Microsoft.EntityFrameworkCore.SqlServer.csproj" />
1211
<ProjectReference Include="..\..\Playground.ServiceDefaults\Playground.ServiceDefaults.csproj" />
1312
</ItemGroup>
1413

Original file line numberDiff line numberDiff line change
@@ -1,60 +1,56 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using Microsoft.Azure.Cosmos;
5-
using Newtonsoft.Json;
4+
using Microsoft.EntityFrameworkCore;
65

76
var builder = WebApplication.CreateBuilder(args);
87

98
int insertionRows = builder.Configuration.GetValue<int>("InsertionRows", 1);
109

1110
builder.AddServiceDefaults();
12-
builder.AddAzureCosmosDB("db", settings =>
13-
{
14-
settings.IgnoreEmulatorCertificate = true;
15-
});
11+
12+
builder.AddSqlServerDbContext<MyDbContext>("db");
1613

1714
var app = builder.Build();
1815

19-
app.MapGet("/", async (CosmosClient cosmosClient) =>
16+
app.MapGet("/", async (MyDbContext context) =>
2017
{
21-
var db = (await cosmosClient.CreateDatabaseIfNotExistsAsync("db")).Database;
22-
var container = (await db.CreateContainerIfNotExistsAsync("entries", "/Id")).Container;
18+
// You wouldn't normally do this on every call,
19+
// but doing it here just to make this simple.
20+
context.Database.EnsureCreated();
2321

24-
// Just demonstrating the use of a non secret parameter from
25-
// the AddParameter method in the app host.
26-
for (var row = 0; row < insertionRows; row++)
22+
for (var i = 0; i < insertionRows; i++)
2723
{
28-
var newEntry = new Entry() { Id = Guid.NewGuid().ToString() };
29-
await container.CreateItemAsync(newEntry);
24+
var entry = new Entry();
25+
await context.Entries.AddAsync(entry);
3026
}
3127

32-
var entries = new List<Entry>();
33-
var iterator = container.GetItemQueryIterator<Entry>(requestOptions: new QueryRequestOptions() { MaxItemCount = 5 });
28+
await context.SaveChangesAsync();
3429

35-
var batchCount = 0;
36-
while (iterator.HasMoreResults)
37-
{
38-
batchCount++;
39-
var batch = await iterator.ReadNextAsync();
40-
foreach (var entry in batch)
41-
{
42-
entries.Add(entry);
43-
}
44-
}
30+
var entries = await context.Entries.ToListAsync();
4531

4632
return new
4733
{
48-
batchCount = batchCount,
4934
totalEntries = entries.Count,
5035
entries = entries
5136
};
5237
});
5338

5439
app.Run();
5540

41+
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options)
42+
{
43+
protected override void OnModelCreating(ModelBuilder modelBuilder)
44+
{
45+
base.OnModelCreating(modelBuilder);
46+
47+
modelBuilder.Entity<Entry>().HasKey(e => e.Id);
48+
}
49+
50+
public DbSet<Entry> Entries { get; set; }
51+
}
52+
5653
public class Entry
5754
{
58-
[JsonProperty("id")]
59-
public string? Id { get; set; }
55+
public Guid Id { get; set; } = Guid.NewGuid();
6056
}

playground/ParameterEndToEnd/ParameterEndToEnd.AppHost/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
var db = builder.Environment.EnvironmentName switch
2222
{
2323
"Production" => builder.AddConnectionString("db"),
24-
_ => simulateProduction ? builder.AddConnectionString("db") : builder.AddAzureCosmosDB("cosmos").UseEmulator().AddDatabase("db")
24+
_ => simulateProduction ? builder.AddConnectionString("db") : builder.AddSqlServer("sql").AddDatabase("db")
2525
};
2626

2727
var insertionrows = builder.AddParameter("insertionrows");

playground/ParameterEndToEnd/ParameterEndToEnd.AppHost/Properties/launchSettings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "http://json.schemastore.org/launchsettings.json",
33
"profiles": {
4-
"http-UseEmulator": {
4+
"http-UseContainer": {
55
"commandName": "Project",
66
"dotnetRunMessages": true,
77
"launchBrowser": true,

0 commit comments

Comments
 (0)