-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMysqlDbContext.cs
More file actions
50 lines (40 loc) · 1.84 KB
/
MysqlDbContext.cs
File metadata and controls
50 lines (40 loc) · 1.84 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.EntityFrameworkCore;
using Xunit.Abstractions;
namespace CommunityToolkit.Datasync.TestCommon.Databases;
[ExcludeFromCodeCoverage]
public class MysqlDbContext(DbContextOptions<MysqlDbContext> options) : BaseDbContext<MysqlDbContext, MysqlEntityMovie>(options)
{
public static async Task<MysqlDbContext> CreateContextAsync(string connectionString, ITestOutputHelper output = null, bool clearEntities = true)
{
if (string.IsNullOrEmpty(connectionString))
{
throw new ArgumentNullException(nameof(connectionString));
}
DbContextOptionsBuilder<MysqlDbContext> optionsBuilder = new DbContextOptionsBuilder<MysqlDbContext>()
.UseMySql(connectionString: connectionString, serverVersion: ServerVersion.AutoDetect(connectionString), options => options.EnableRetryOnFailure())
.EnableLogging(output);
MysqlDbContext context = new(optionsBuilder.Options);
await context.InitializeDatabaseAsync(clearEntities);
await context.PopulateDatabaseAsync();
return context;
}
internal async Task InitializeDatabaseAsync(bool clearEntities)
{
await Database.EnsureCreatedAsync();
if (clearEntities)
{
await ExecuteRawSqlOnEachEntityAsync("DELETE FROM {0}");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MysqlEntityMovie>().Property(m => m.UpdatedAt)
.ValueGeneratedOnAddOrUpdate();
modelBuilder.Entity<MysqlEntityMovie>().Property(m => m.Version)
.IsRowVersion();
base.OnModelCreating(modelBuilder);
}
}