|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 |
| -using Microsoft.Azure.Cosmos; |
5 |
| -using Newtonsoft.Json; |
| 4 | +using Microsoft.EntityFrameworkCore; |
6 | 5 |
|
7 | 6 | var builder = WebApplication.CreateBuilder(args);
|
8 | 7 |
|
9 | 8 | int insertionRows = builder.Configuration.GetValue<int>("InsertionRows", 1);
|
10 | 9 |
|
11 | 10 | builder.AddServiceDefaults();
|
12 |
| -builder.AddAzureCosmosDB("db", settings => |
13 |
| -{ |
14 |
| - settings.IgnoreEmulatorCertificate = true; |
15 |
| -}); |
| 11 | + |
| 12 | +builder.AddSqlServerDbContext<MyDbContext>("db"); |
16 | 13 |
|
17 | 14 | var app = builder.Build();
|
18 | 15 |
|
19 |
| -app.MapGet("/", async (CosmosClient cosmosClient) => |
| 16 | +app.MapGet("/", async (MyDbContext context) => |
20 | 17 | {
|
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(); |
23 | 21 |
|
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++) |
27 | 23 | {
|
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); |
30 | 26 | }
|
31 | 27 |
|
32 |
| - var entries = new List<Entry>(); |
33 |
| - var iterator = container.GetItemQueryIterator<Entry>(requestOptions: new QueryRequestOptions() { MaxItemCount = 5 }); |
| 28 | + await context.SaveChangesAsync(); |
34 | 29 |
|
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(); |
45 | 31 |
|
46 | 32 | return new
|
47 | 33 | {
|
48 |
| - batchCount = batchCount, |
49 | 34 | totalEntries = entries.Count,
|
50 | 35 | entries = entries
|
51 | 36 | };
|
52 | 37 | });
|
53 | 38 |
|
54 | 39 | app.Run();
|
55 | 40 |
|
| 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 | + |
56 | 53 | public class Entry
|
57 | 54 | {
|
58 |
| - [JsonProperty("id")] |
59 |
| - public string? Id { get; set; } |
| 55 | + public Guid Id { get; set; } = Guid.NewGuid(); |
60 | 56 | }
|
0 commit comments