-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathDemoDbContext.cs
More file actions
106 lines (82 loc) · 3.4 KB
/
Copy pathDemoDbContext.cs
File metadata and controls
106 lines (82 loc) · 3.4 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using DbContextExtensionsExamples.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using System;
namespace DbContextExtensionsExamples;
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; }
public DbSet<Blog> Blogs { get; set; }
public DbSet<RssBlog> RssBlogs { get; set; }
public DbSet<ComplexTypeOrder> ComplexTypeOrders { get; set; }
public DbSet<OwnedTypeOrder> OwnedTypeOrders { get; set; }
public DbSet<ComplexOwnedTypeOrder> ComplexOwnedTypeOrders { get; set; }
public DbSet<JsonComplexTypeOrder> JsonComplexTypeOrders { get; set; }
public DbSet<JsonOwnedTypeOrder> JsonOwnedTypeOrders { get; set; }
public DbSet<JsonComplexOwnedTypeOrder> JsonComplexOwnedTypeOrders { 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.Key).HasColumnName("Key1");
modelBuilder.Entity<ConfigurationEntry>().Property(x => x.Id).HasColumnName("Id1");
modelBuilder.Entity<ConfigurationEntry>().Property(x => x.SeasonAsString).HasConversion(v => v.ToString(), v => (Season)Enum.Parse(typeof(Season), v));
modelBuilder.Entity<JsonComplexTypeOrder>().ComplexProperty(x => x.ShippingAddress, x =>
{
x.ToJson();
//x.ToJson("xxx").HasColumnType("json");
x.ComplexProperty(y => y.Location, y =>
{
y.HasJsonPropertyName("xxx");
});
});
modelBuilder.Entity<JsonOwnedTypeOrder>().OwnsOne(x => x.ShippingAddress, x =>
{
x.ToJson();
//x.ToJson("xxx").HasColumnType("json");
x.OwnsOne(y => y.Location, y =>
{
y.HasJsonPropertyName("xxx");
});
});
modelBuilder.Entity<JsonComplexOwnedTypeOrder>().ComplexProperty(x => x.ComplexShippingAddress, x =>
{
x.ToJson();
//x.ToJson("xxx").HasColumnType("json");
x.ComplexProperty(y => y.Location, y =>
{
y.HasJsonPropertyName("xxx");
});
});
modelBuilder.Entity<JsonComplexOwnedTypeOrder>().OwnsOne(x => x.OwnedShippingAddress, x =>
{
x.ToJson();
//x.ToJson("xxx").HasColumnType("json");
x.OwnsOne(y => y.Location, y =>
{
y.HasJsonPropertyName("xxx");
});
});
base.OnModelCreating(modelBuilder);
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
public class RssBlog : Blog
{
public string RssUrl { get; set; }
}