Skip to content

Commit

Permalink
1. 给 ISeedDataInitializer 添加一个 EntityType 属性 #179
Browse files Browse the repository at this point in the history
2. 在种子数据初始化的时候筛选出当前上下文的种子数据初始化器进行初始化 #179
  • Loading branch information
gmf520 committed Aug 21, 2020
1 parent 4960eb4 commit 2aa7b48
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/OSharp.EntityFrameworkCore/MigrationPackBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -62,8 +63,11 @@ public override void UsePack(IServiceProvider provider)
}
}

//种子数据
var seedDataInitializers = provider.GetServices<ISeedDataInitializer>().OrderBy(m => m.Order);
//初始化种子数据,只初始化当前上下文的种子数据
IEntityManager entityManager = provider.GetService<IEntityManager>();
Type[] entityTypes = entityManager.GetEntityRegisters(typeof(TDbContext)).Select(m => m.EntityType).Distinct().ToArray();
IEnumerable<ISeedDataInitializer> seedDataInitializers = provider.GetServices<ISeedDataInitializer>()
.Where(m => entityTypes.Contains(m.EntityType)).OrderBy(m => m.Order);
foreach (ISeedDataInitializer initializer in seedDataInitializers)
{
initializer.Initialize();
Expand Down
7 changes: 7 additions & 0 deletions src/OSharp/Entity/ISeedDataInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// <last-date>2020-03-06 21:36</last-date>
// -----------------------------------------------------------------------

using System;

using OSharp.Dependency;


Expand All @@ -23,6 +25,11 @@ public interface ISeedDataInitializer
/// </summary>
int Order { get; }

/// <summary>
/// 获取 所属实体类型
/// </summary>
Type EntityType { get; }

/// <summary>
/// 初始化种子数据
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/OSharp/Entity/SeedDataInitializerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ protected SeedDataInitializerBase(IServiceProvider rootProvider)
/// </summary>
public virtual int Order => 0;

/// <summary>
/// 获取 所属实体类型
/// </summary>
public Type EntityType => typeof(TEntity);

/// <summary>
/// 初始化种子数据
/// </summary>
Expand Down

0 comments on commit 2aa7b48

Please sign in to comment.