-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Program.cs
52 lines (39 loc) · 1.41 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Linq;
using EFCore.Trimming.Tests;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
ctx.Add(new Blog { Name = "Some Blog Name" });
await ctx.SaveChangesAsync();
ctx.ChangeTracker.Clear();
// Execute any query to make sure the basic query pipeline works
var blog = await ctx.Blogs.Where(b => b.Name.StartsWith("Some ")).SingleAsync();
if (blog.Name != "Some Blog Name")
{
throw new Exception($"Incorrect blog name ({blog.Name})");
}
Console.WriteLine("Database query executed successfully.");
public class BlogContext : DbContext
{
public BlogContext()
=> Blogs = Set<Blog>();
private static readonly string ConnectionString;
public DbSet<Blog> Blogs { get; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(ConnectionString);
static BlogContext()
{
var builder = new SqlConnectionStringBuilder(TestEnvironment.DefaultConnection) { InitialCatalog = "TrimmingTests" };
ConnectionString = builder.ToString();
}
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
}