-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDemoDbContext.cs
More file actions
39 lines (29 loc) · 1.46 KB
/
Copy pathDemoDbContext.cs
File metadata and controls
39 lines (29 loc) · 1.46 KB
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
using ConnectionExtensionsExamples.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using System;
namespace ConnectionExtensionsExamples;
public class DemoDbContext : DbContext
{
private string _connectionString = ConnectionStrings.SqlServerConnectionString;
public DbSet<Row> Rows { get; set; }
public DbSet<CompositeKeyRow> CompositeKeyRows { get; set; }
public DbSet<ConfigurationEntry> ConfigurationEntries { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connectionString);
#if NET9_0_OR_GREATER
optionsBuilder.ConfigureWarnings(x => x.Log([RelationalEventId.PendingModelChangesWarning]));
#endif
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CompositeKeyRow>().HasKey(x => new { x.Id1, x.Id2 });
modelBuilder.Entity<ConfigurationEntry>().Property(x => x.Id).HasDefaultValueSql("newsequentialid()");
modelBuilder.Entity<ConfigurationEntry>().Property(x => x.Id).HasColumnName("Id1");
modelBuilder.Entity<ConfigurationEntry>().Property(x => x.Key).HasColumnName("Key1");
modelBuilder.Entity<ConfigurationEntry>().Property(x => x.SeasonAsString).HasConversion(v => v.ToString(), v => (Season)Enum.Parse(typeof(Season), v));
base.OnModelCreating(modelBuilder);
}
}