CSharp: donet 6 database create view with EF Core 6

 

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
[Keyless]
public class PersonOrderCount
{
    public string Name { get; set; }
    public int Count { get; set; }
}
public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
 
    public ICollection<Order> Orders { get; set; }
}
public class Order
{
    public int OrderId { get; set; }
    public int PersonId { get; set; }
    public string Description { get; set; }
    public int Price { get; set; }
 
    public Person Person { get; set; }
}
 
 
class ApplicationDbContext : DbContext
{
    public DbSet<Person> Persons { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<PersonOrderCount> PersonOrderCounts { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
 
        modelBuilder.Entity<Person>()
            .HasMany(p => p.Orders)
            .WithOne(o => o.Person)
            .HasForeignKey(o => o.PersonId);
        //创建视图
        modelBuilder.Entity<PersonOrderCount>()
            .HasNoKey()
            .ToView("vw_PersonOrderCount");
    }
 
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=DESKTOP-NQK85G5\\GEOVIN2008;Database=geovinduDBA;User ID=SA;Password=geovindu;TrustServerCertificate=True");
    }
}
 
  [DbContext(typeof(ApplicationDbContext))]
    partial class ApplicationDbContextModelSnapshot : ModelSnapshot
    {
        protected override void BuildModel(ModelBuilder modelBuilder)
        {
#pragma warning disable 612, 618
            modelBuilder
                .HasAnnotation("ProductVersion", "7.0.0")
                .HasAnnotation("Relational:MaxIdentifierLength", 128);
 
            SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
 
            modelBuilder.Entity("Order", b =>
                {
                    b.Property<int>("OrderId")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("int");
 
                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("OrderId"));
 
                    b.Property<string>("Description")
                        .IsRequired()
                        .HasColumnType("nvarchar(max)");
 
                    b.Property<int>("PersonId")
                        .HasColumnType("int");
 
                    b.Property<int>("Price")
                        .HasColumnType("int");
 
                    b.HasKey("OrderId");
 
                    b.HasIndex("PersonId");
 
                    b.ToTable("Orders");
 
                    b.HasData(
                        new
                        {
                            OrderId = 1,
                            Description = "...",
                            PersonId = 1,
                            Price = 10
                        },
                        new
                        {
                            OrderId = 2,
                            Description = "...",
                            PersonId = 2,
                            Price = 20
                        },
                        new
                        {
                            OrderId = 3,
                            Description = "...",
                            PersonId = 4,
                            Price = 30
                        },
                        new
                        {
                            OrderId = 4,
                            Description = "...",
                            PersonId = 5,
                            Price = 40
                        },
                        new
                        {
                            OrderId = 5,
                            Description = "...",
                            PersonId = 1,
                            Price = 50
                        },
                        new
                        {
                            OrderId = 6,
                            Description = "...",
                            PersonId = 6,
                            Price = 60
                        },
                        new
                        {
                            OrderId = 7,
                            Description = "...",
                            PersonId = 7,
                            Price = 70
                        },
                        new
                        {
                            OrderId = 8,
                            Description = "...",
                            PersonId = 1,
                            Price = 80
                        },
                        new
                        {
                            OrderId = 9,
                            Description = "...",
                            PersonId = 8,
                            Price = 90
                        },
                        new
                        {
                            OrderId = 10,
                            Description = "...",
                            PersonId = 9,
                            Price = 100
                        },
                        new
                        {
                            OrderId = 11,
                            Description = "...",
                            PersonId = 1,
                            Price = 110
                        },
                        new
                        {
                            OrderId = 12,
                            Description = "...",
                            PersonId = 2,
                            Price = 120
                        },
                        new
                        {
                            OrderId = 13,
                            Description = "...",
                            PersonId = 2,
                            Price = 130
                        },
                        new
                        {
                            OrderId = 14,
                            Description = "...",
                            PersonId = 3,
                            Price = 140
                        },
                        new
                        {
                            OrderId = 15,
                            Description = "...",
                            PersonId = 1,
                            Price = 150
                        },
                        new
                        {
                            OrderId = 16,
                            Description = "...",
                            PersonId = 4,
                            Price = 160
                        },
                        new
                        {
                            OrderId = 17,
                            Description = "...",
                            PersonId = 1,
                            Price = 170
                        },
                        new
                        {
                            OrderId = 18,
                            Description = "...",
                            PersonId = 1,
                            Price = 180
                        },
                        new
                        {
                            OrderId = 19,
                            Description = "...",
                            PersonId = 5,
                            Price = 190
                        },
                        new
                        {
                            OrderId = 20,
                            Description = "...",
                            PersonId = 6,
                            Price = 200
                        },
                        new
                        {
                            OrderId = 21,
                            Description = "...",
                            PersonId = 1,
                            Price = 210
                        },
                        new
                        {
                            OrderId = 22,
                            Description = "...",
                            PersonId = 7,
                            Price = 220
                        },
                        new
                        {
                            OrderId = 23,
                            Description = "...",
                            PersonId = 7,
                            Price = 230
                        },
                        new
                        {
                            OrderId = 24,
                            Description = "...",
                            PersonId = 8,
                            Price = 240
                        },
                        new
                        {
                            OrderId = 25,
                            Description = "...",
                            PersonId = 1,
                            Price = 250
                        },
                        new
                        {
                            OrderId = 26,
                            Description = "...",
                            PersonId = 1,
                            Price = 260
                        },
                        new
                        {
                            OrderId = 27,
                            Description = "...",
                            PersonId = 9,
                            Price = 270
                        },
                        new
                        {
                            OrderId = 28,
                            Description = "...",
                            PersonId = 9,
                            Price = 280
                        },
                        new
                        {
                            OrderId = 29,
                            Description = "...",
                            PersonId = 9,
                            Price = 290
                        },
                        new
                        {
                            OrderId = 30,
                            Description = "...",
                            PersonId = 2,
                            Price = 300
                        },
                        new
                        {
                            OrderId = 31,
                            Description = "...",
                            PersonId = 3,
                            Price = 310
                        },
                        new
                        {
                            OrderId = 32,
                            Description = "...",
                            PersonId = 1,
                            Price = 320
                        },
                        new
                        {
                            OrderId = 33,
                            Description = "...",
                            PersonId = 1,
                            Price = 330
                        },
                        new
                        {
                            OrderId = 34,
                            Description = "...",
                            PersonId = 1,
                            Price = 340
                        },
                        new
                        {
                            OrderId = 35,
                            Description = "...",
                            PersonId = 5,
                            Price = 350
                        },
                        new
                        {
                            OrderId = 36,
                            Description = "...",
                            PersonId = 1,
                            Price = 360
                        },
                        new
                        {
                            OrderId = 37,
                            Description = "...",
                            PersonId = 5,
                            Price = 370
                        },
                        new
                        {
                            OrderId = 38,
                            Description = "...",
                            PersonId = 1,
                            Price = 380
                        },
                        new
                        {
                            OrderId = 39,
                            Description = "...",
                            PersonId = 1,
                            Price = 390
                        },
                        new
                        {
                            OrderId = 40,
                            Description = "...",
                            PersonId = 1,
                            Price = 400
                        });
                });
 
            modelBuilder.Entity("Person", b =>
                {
                    b.Property<int>("PersonId")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("int");
 
                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("PersonId"));
 
                    b.Property<string>("Name")
                        .IsRequired()
                        .HasColumnType("nvarchar(max)");
 
                    b.HasKey("PersonId");
 
                    b.ToTable("Persons");
 
                    b.HasData(
                        new
                        {
                            PersonId = 1,
                            Name = "Ayşe"
                        },
                        new
                        {
                            PersonId = 2,
                            Name = "Hilmi"
                        },
                        new
                        {
                            PersonId = 3,
                            Name = "Raziye"
                        },
                        new
                        {
                            PersonId = 4,
                            Name = "Süleyman"
                        },
                        new
                        {
                            PersonId = 5,
                            Name = "Fadime"
                        },
                        new
                        {
                            PersonId = 6,
                            Name = "Şuayip"
                        },
                        new
                        {
                            PersonId = 7,
                            Name = "geovindu"
                        },
                        new
                        {
                            PersonId = 8,
                            Name = "Geovin Du"
                        },
                        new
                        {
                            PersonId = 9,
                            Name = "涂聚文"
                        },
                        new
                        {
                            PersonId = 10,
                            Name = "Muaviye"
                        });
                });
 
            modelBuilder.Entity("PersonOrderCount", b =>
                {
                    b.Property<int>("Count")
                        .HasColumnType("int");
 
                    b.Property<string>("Name")
                        .IsRequired()
                        .HasColumnType("nvarchar(max)");
 
                    b.ToTable((string)null);
 
                    b.ToView("vw_PersonOrderCount", (string)null);
                });
 
            modelBuilder.Entity("Order", b =>
                {
                    b.HasOne("Person", "Person")
                        .WithMany("Orders")
                        .HasForeignKey("PersonId")
                        .OnDelete(DeleteBehavior.Cascade)
                        .IsRequired();
 
                    b.Navigation("Person");
                });
 
            modelBuilder.Entity("Person", b =>
                {
                    b.Navigation("Orders");
                });
#pragma warning restore 612, 618
        }
    }

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
//创建表和初始化数据
ApplicationDbContext context = new();
 
await context.Database.MigrateAsync();
 
 
var datas = await context.PersonOrderCounts.ToListAsync();
foreach(var data in datas)
{
    Console.WriteLine(data.Name+"\t\n");
}
Console.WriteLine();

  

 输出:

 

posted @   ®Geovin Du Dream Park™  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-02-04 java: imap Receive Email
2009-02-04 C#2.0 从sql server 中读取二进制图片
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示