Skip to content

Commit

Permalink
添加批量新增方法
Browse files Browse the repository at this point in the history
  • Loading branch information
ren8179 committed Oct 21, 2020
1 parent babbce8 commit a081852
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
核心类库,部分代码参考自[abp](https://github.com/abpframework/abp)项目,用来实现框架的基础功能

你可以运行以下命令在你的项目中使用类库
> PM> Install-Package Qf.Core -Version 1.0.3.4
> PM> Install-Package Qf.Core -Version 1.0.3.5
* **Qf.Core.AutoMapper**

Expand All @@ -27,7 +27,7 @@
基于EntityFrameworkCore的仓储基类,默认使用统一工作单元,自动注入默认仓储.

你可以运行以下命令在你的项目中使用类库
> PM> Install-Package Qf.Core.EFCore -Version 1.0.3.6
> PM> Install-Package Qf.Core.EFCore -Version 1.0.3.7
* **Qf.Core.Web**

Expand Down
10 changes: 5 additions & 5 deletions framework/src/Qf.Core.EFCore/Qf.Core.EFCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
<RepositoryUrl>https://github.com/ren8179/Qf.Core</RepositoryUrl>
<PackageLicenseFile>README.md</PackageLicenseFile>
<PackageIcon>icon.png</PackageIcon>
<Version>1.0.3.6</Version>
<AssemblyVersion>1.0.3.6</AssemblyVersion>
<FileVersion>1.0.3.6</FileVersion>
<Version>1.0.3.7</Version>
<AssemblyVersion>1.0.3.7</AssemblyVersion>
<FileVersion>1.0.3.7</FileVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.8" />
<PackageReference Include="Qf.Core" Version="1.0.3.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.9" />
<PackageReference Include="Qf.Core" Version="1.0.3.5" />
</ItemGroup>

<ItemGroup>
Expand Down
19 changes: 18 additions & 1 deletion framework/src/Qf.Core.EFCore/Repositories/EfCoreRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public override TEntity Add(TEntity entity, bool autoSave = false)

return savedEntity;
}
public override void AddRange(List<TEntity> entities, bool autoSave = false)
{
DbSet.AddRange(entities);
if (autoSave)
{
DbContext.SaveChanges();
}
}
public override async Task<TEntity> AddAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
var savedEntity = DbSet.Add(entity).Entity;
Expand All @@ -49,6 +57,15 @@ public override async Task<TEntity> AddAsync(TEntity entity, bool autoSave = fal

return savedEntity;
}
public override async Task AddRangeAsync(List<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
await DbSet.AddRangeAsync(entities, GetCancellationToken(cancellationToken));

if (autoSave)
{
await DbContext.SaveChangesAsync(GetCancellationToken(cancellationToken));
}
}

public override TEntity Update(TEntity entity, bool autoSave = false)
{
Expand Down Expand Up @@ -121,7 +138,7 @@ public override async Task DelAsync(TEntity entity, bool autoSave = false, Cance
public override async Task DelAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false, CancellationToken cancellationToken = default)
{
var entity = await DbSet.AsQueryable().Where(predicate).ToListAsync(GetCancellationToken(cancellationToken));
if (entity == null || entity.Count<1)
if (entity == null || entity.Count < 1)
return;
DbSet.RemoveRange(entity);

Expand Down
2 changes: 2 additions & 0 deletions framework/src/Qf.Core/Infrastructure/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public interface IRepository<TEntity, TKey> : IRepository where TEntity : IEntit
{

TEntity Add([NotNull]TEntity model, bool autoSave = false);
void AddRange([NotNull] List<TEntity> entitys, bool autoSave = false);
Task<TEntity> AddAsync([NotNull] TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
Task AddRangeAsync([NotNull] List<TEntity> entitys, bool autoSave = false, CancellationToken cancellationToken = default);
TEntity Update([NotNull]TEntity model, bool autoSave = false);
Task<TEntity> UpdateAsync([NotNull] TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
void Del([NotNull]TEntity model, bool autoSave = false);
Expand Down
8 changes: 6 additions & 2 deletions framework/src/Qf.Core/Infrastructure/RepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -25,12 +24,17 @@ protected RepositoryBase()
}

public abstract TEntity Add(TEntity model, bool autoSave = false);
public abstract void AddRange(List<TEntity> entitys, bool autoSave = false);

public virtual Task<TEntity> AddAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
return Task.FromResult(Add(entity, autoSave));
}

public virtual Task AddRangeAsync(List<TEntity> entitys, bool autoSave = false, CancellationToken cancellationToken = default)
{
AddRange(entitys, autoSave);
return Task.CompletedTask;
}
public abstract TEntity Update(TEntity model, bool autoSave = false);
public virtual Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
Expand Down
14 changes: 7 additions & 7 deletions framework/src/Qf.Core/Qf.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
<RepositoryUrl>https://github.com/ren8179/Qf.Core</RepositoryUrl>
<PackageIcon>icon.png</PackageIcon>
<PackageLicenseFile>README.md</PackageLicenseFile>
<Version>1.0.3.4</Version>
<AssemblyVersion>1.0.3.4</AssemblyVersion>
<FileVersion>1.0.3.4</FileVersion>
<Version>1.0.3.5</Version>
<AssemblyVersion>1.0.3.5</AssemblyVersion>
<FileVersion>1.0.3.5</FileVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac.Extras.DynamicProxy" Version="5.0.0" />
<PackageReference Include="Autofac.Extras.DynamicProxy" Version="6.0.0" />
<PackageReference Include="Castle.Core" Version="4.4.1" />
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.8" />
<PackageReference Include="Nito.AsyncEx.Context" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.9" />
<PackageReference Include="Nito.AsyncEx.Context" Version="5.1.0" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
</ItemGroup>

Expand Down

0 comments on commit a081852

Please sign in to comment.