schangxiang@126.com
2025-05-21 a3a2b238a2626ef8744e7a135f9ca2e2fbb5184c
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
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Uow;
 
namespace CMS.Plugin.PipeLineLems.Domain.MyTestEntityNames
{
    /// <summary>
    /// MyTestEntityName种子数据提供程序
    /// </summary>
    public class MyTestEntityNameDataSeedContributor : IDataSeedContributor, ITransientDependency
    {
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly IMyTestEntityNameRepository _mytestentitynameRepository;
        private readonly IGuidGenerator _guidGenerator;
 
        /// <summary>
        /// Initializes a new instance of the <see cref="MyTestEntityNameDataSeedContributor"/> class.
        /// </summary>
        /// <param name="unitOfWorkManager">The unit of work manager.</param>
        /// <param name="guidGenerator">The unique identifier generator.</param>
        /// <param name="mytestentitynameRepository">The work section repository.</param>
        public MyTestEntityNameDataSeedContributor(IUnitOfWorkManager unitOfWorkManager,  IGuidGenerator guidGenerator, IMyTestEntityNameRepository mytestentitynameRepository)
        {
            _unitOfWorkManager = unitOfWorkManager;
            _mytestentitynameRepository = mytestentitynameRepository;
            _guidGenerator = guidGenerator;
        }
 
        /// <inheritdoc />
        public async Task SeedAsync(DataSeedContext context)
        {
            if (context.Properties.ContainsKey(CMSPluginDbProperties.ConnectionStringName) && context.Properties[CMSPluginDbProperties.ConnectionStringName]?.ToString() == CMSPluginDbProperties.ConnectionStringName)
            {
                try
                {
                    //using var unitofWork = _unitOfWorkManager.Begin(requiresNew: true);
                    //await unitofWork.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
 
            if (context.Properties.ContainsKey("SeedTestData") && context.Properties["SeedTestData"]?.ToString() == "SeedTestData")
            {
                try
                {
                    await SeedMyTestEntityNameDataAsync();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
 
        /// <summary>
        /// Seeds the work section data asynchronous.
        /// </summary>
        private async Task SeedMyTestEntityNameDataAsync()
        {
            using var unitofWork = _unitOfWorkManager.Begin(requiresNew: true);
            if (await _mytestentitynameRepository.GetCountAsync() == 0)
            {
                var MyTestEntityNames = new List<MyTestEntityName>();
                for (int i = 1; i <= 80; i++)
                {
                    var mytestentityname = new MyTestEntityName(_guidGenerator.Create(), $"MyTestEntityName_Code{i}", $"MyTestEntityName_Name{i}", i, $"MyTestEntityName_Remark{i}");
                    MyTestEntityNames.Add(mytestentityname);
                }
 
                await _mytestentitynameRepository.InsertManyAsync(MyTestEntityNames);
                await unitofWork.SaveChangesAsync();
            }
        }
    }
}