Skip to content

Commit

Permalink
添加对.net core3.1和.net 6的支持。同时删除update的lamda表达的支持,此方法在实际的生产中并不太实用。
Browse files Browse the repository at this point in the history
  • Loading branch information
longxianghui committed May 25, 2022
1 parent d22cf02 commit 911be4c
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 58 deletions.
16 changes: 8 additions & 8 deletions Leo.Chimp.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.705
# Visual Studio Version 16
VisualStudioVersion = 16.0.32228.343
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leo.Chimp", "src\Leo.Chimp\Leo.Chimp.csproj", "{AE57FE9E-110F-4BBD-AF58-243A37E334C3}"
EndProject
Expand All @@ -15,7 +15,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leo.Chimp.Example", "exampl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leo.Chimp.Domain", "example\Leo.Chimp.Domain\Leo.Chimp.Domain.csproj", "{1216801F-2F9A-423D-9460-E3D301496533}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leo.Chimp.Test", "test\Leo.Chimp.Test\Leo.Chimp.Test.csproj", "{ACAEE484-293F-4F82-B8D6-269A00FDBB57}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leo.Chimp.Test", "test\Leo.Chimp.Test\Leo.Chimp.Test.csproj", "{1B160D6D-A698-4465-9F9F-768D7248AB3B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -35,10 +35,10 @@ Global
{1216801F-2F9A-423D-9460-E3D301496533}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1216801F-2F9A-423D-9460-E3D301496533}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1216801F-2F9A-423D-9460-E3D301496533}.Release|Any CPU.Build.0 = Release|Any CPU
{ACAEE484-293F-4F82-B8D6-269A00FDBB57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACAEE484-293F-4F82-B8D6-269A00FDBB57}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACAEE484-293F-4F82-B8D6-269A00FDBB57}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACAEE484-293F-4F82-B8D6-269A00FDBB57}.Release|Any CPU.Build.0 = Release|Any CPU
{1B160D6D-A698-4465-9F9F-768D7248AB3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B160D6D-A698-4465-9F9F-768D7248AB3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B160D6D-A698-4465-9F9F-768D7248AB3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B160D6D-A698-4465-9F9F-768D7248AB3B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -47,7 +47,7 @@ Global
{AE57FE9E-110F-4BBD-AF58-243A37E334C3} = {A9856CFD-8B10-4C9E-B4B7-E49704894C71}
{22CF40A1-EE7E-4EBA-A502-5C5FC0D0758B} = {98E27906-6784-4538-9CDA-90FDEA7C83EF}
{1216801F-2F9A-423D-9460-E3D301496533} = {98E27906-6784-4538-9CDA-90FDEA7C83EF}
{ACAEE484-293F-4F82-B8D6-269A00FDBB57} = {8DC16EBA-2D9F-473A-820E-5A96C35E44F5}
{1B160D6D-A698-4465-9F9F-768D7248AB3B} = {8DC16EBA-2D9F-473A-820E-5A96C35E44F5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D936E593-6A06-4989-8708-382308D741CF}
Expand Down
45 changes: 29 additions & 16 deletions src/Leo.Chimp/EfCoreRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Data;
Expand Down Expand Up @@ -31,14 +32,25 @@ public TEntity GetById(object id)

return Entities.Find(id);
}

#if NETSTANDARD2_0
public Task<TEntity> GetByIdAsync(object id)
{
if (id == null)
throw new ArgumentNullException("id");

return Entities.FindAsync(id);
}
#endif
#if NETSTANDARD2_1 || NET6_0
public Task<TEntity> GetByIdAsync(object id)
{
if (id == null)
throw new ArgumentNullException("id");

return Entities.FindAsync(id).AsTask();
}
#endif

public void Insert(TEntity entity)
{
Expand All @@ -48,21 +60,33 @@ public void Insert(TEntity entity)
Entities.Add(entity);
}


public void Insert(IEnumerable<TEntity> entities)
{
if (!entities.Any())
throw new ArgumentNullException("entities");

Entities.AddRange(entities);
}

#if NETSTANDARD2_0
public Task InsertAsync(TEntity entity)
{
if (entity == null)
throw new ArgumentNullException("entity");

return Entities.AddAsync(entity);
}
#endif
#if NETSTANDARD2_1 || NET6_0
public Task InsertAsync(TEntity entity)
{
if (entity == null)
throw new ArgumentNullException("entity");

return Entities.AddAsync(entity).AsTask();

}
#endif

public Task InsertAsync(IEnumerable<TEntity> entities)
{
Expand Down Expand Up @@ -90,19 +114,6 @@ public void Update(IEnumerable<TEntity> entities)

}

public void Update(TEntity entity, params Expression<Func<TEntity, object>>[] properties)
{
foreach (var property in properties)
{
var propertyName = property.Name;
if (string.IsNullOrEmpty(propertyName))
{
propertyName = GetPropertyName(property.Body.ToString());
}
_context.Entry(entity).Property(propertyName).IsModified = true;

}
}

string GetPropertyName(string str)
{
Expand Down Expand Up @@ -134,7 +145,7 @@ public void Delete(Expression<Func<TEntity, bool>> predicate)
_context.RemoveRange(Entities.Where(predicate));
}

#region Properties
#region Properties

/// <summary>
/// Gets a table
Expand All @@ -151,7 +162,7 @@ public void Delete(Expression<Func<TEntity, bool>> predicate)
/// </summary>
protected virtual DbSet<TEntity> Entities => _entities ?? (_entities = _context.Set<TEntity>());

#endregion
#endregion
private void AttachIfNot(TEntity entity)
{
var entry = _context.ChangeTracker.Entries().FirstOrDefault(ent => ent.Entity == entity);
Expand All @@ -161,5 +172,7 @@ private void AttachIfNot(TEntity entity)
}
_context.Attach(entity);
}


}
}
9 changes: 2 additions & 7 deletions src/Leo.Chimp/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace Leo.Chimp
{
Expand Down Expand Up @@ -56,6 +57,7 @@ public interface IRepository<TEntity> where TEntity : class
/// </summary>
/// <param name="entities">Inserted entity</param>
void Insert(IEnumerable<TEntity> entities);

/// <summary>
/// 添加实体
/// </summary>
Expand All @@ -82,13 +84,6 @@ public interface IRepository<TEntity> where TEntity : class
/// <param name="entities"></param>
void Update(IEnumerable<TEntity> entities);

/// <summary>
/// Update
/// </summary>
/// <param name="entity"></param>
/// <param name="properties">Action that can be used to change values of the entity</param>
/// <returns>Updated entity</returns>
void Update(TEntity entity, params Expression<Func<TEntity, object>>[] properties);
#endregion

#region Delete
Expand Down
20 changes: 18 additions & 2 deletions src/Leo.Chimp/Leo.Chimp.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;</TargetFrameworks>
<PackageProjectUrl>https://github.com/longxianghui/chimp</PackageProjectUrl>
<LangVersion>7.3</LangVersion>
<Version>2.1.2</Version>
Expand All @@ -11,12 +11,28 @@
<RepositoryUrl>https://github.com/longxianghui/chimp</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<PackageReference Include="Dapper" Version="2.0.30" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.11" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.4" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.1'">
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.5" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.0-alpha.2" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="5.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />
</ItemGroup>

</Project>
17 changes: 14 additions & 3 deletions src/Leo.Chimp/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
using System.Text;
using Leo.Chimp;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyModel;
using Newtonsoft.Json;



namespace Microsoft.Extensions.DependencyInjection
Expand All @@ -30,6 +29,15 @@ public static IServiceCollection AddChimp<T>(this IServiceCollection services,
return services;
}


#if NETSTANDARD2_1 || NET6_0
public static DbContextOptionsBuilder UseMySql(this DbContextOptionsBuilder optionsBuilder, string connectionString)
{
optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
return optionsBuilder;
}
#endif

#region private function
private static void AddDefault(IServiceCollection services)
{
Expand Down Expand Up @@ -67,6 +75,9 @@ private static IServiceCollection AutoDi(this IServiceCollection services, Type

return services;
}
#endregion



}
#endregion
}
1 change: 1 addition & 0 deletions test/Leo.Chimp.Test/ChimpDbContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ChimpDbContextTest
public ChimpDbContextTest()
{
var services = new ServiceCollection();

services.AddChimp<ChimpDbContext>(opt =>
{
opt.UseMySql("server = 10.0.0.146;database=chimp;uid=root;password=123456;");
Expand Down
2 changes: 1 addition & 1 deletion test/Leo.Chimp.Test/DapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DapperTest
public DapperTest()
{
var services = new ServiceCollection();
services.AddChimp(opt => { opt.UseMySql("server = 10.0.0.146;database=chimp;uid=root;password=123456;"); });
services.AddChimp(opt => { opt.UseMySql("server = 10.0.0.146;database=chimp;uid=root;password=123456;SslMode=none;"); });
//services.AddChimp(opt => { opt.UseSqlServer("Server=10.0.0.99;Database=chimp;Uid=sa;Pwd=Fuluerp123"); });
var sp = services.BuildServiceProvider();
_unitOfWork = sp.GetRequiredService<IUnitOfWork>();
Expand Down
15 changes: 0 additions & 15 deletions test/Leo.Chimp.Test/EfRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,6 @@ public void Updates()
}
}
}
[Fact]
public void UpdateNoSelect()
{
var data = Insert();
var school = new School
{
Id = data.Id,
Name = Guid.NewGuid().ToString()
};
_schoolRepository.Update(school, x => x.Name);
_unitOfWork.SaveChanges();
//这里不能使用 _schoolRepository.GetById(data.Id); 查询出来的结果和数据库不一致
var newSchool = _schoolRepository.TableNoTracking.First(x => x.Id == data.Id);
Assert.True(newSchool.Name == school.Name);
}


[Fact]
Expand Down
22 changes: 16 additions & 6 deletions test/Leo.Chimp.Test/Leo.Chimp.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>

<TargetFramework>net6</TargetFramework>
<!--<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>-->
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Microsoft.Extensions.Dependencyinjection" Version="2.1.1" />
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp2.1'">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Leo.Chimp\Leo.Chimp.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net6'">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>
</Project>

0 comments on commit 911be4c

Please sign in to comment.