Skip to content

Commit

Permalink
Merge pull request #5 from luodaoyi/origin
Browse files Browse the repository at this point in the history
升级到 支持efcore3.1 添加sqlite支持
  • Loading branch information
longxianghui authored May 25, 2022
2 parents d08e24e + 778df97 commit 9f5df71
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 76 deletions.
4 changes: 2 additions & 2 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.30413.136
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 Down
5 changes: 4 additions & 1 deletion example/Leo.Chimp.Domain/Entities/School.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace Leo.Chimp.Domain.Entities
{
[Table("school")]
public class School
{
public School()
{
Students = new List<Student>();
}

[Key]
public Guid Id { get; set; }
public string Name { get; set; }

Expand Down
4 changes: 4 additions & 0 deletions example/Leo.Chimp.Domain/Entities/Student.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
using System.Text.RegularExpressions;

namespace Leo.Chimp.Domain.Entities
{
[Table("student")]
public class Student : IEntity
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public Guid SchoolId { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion example/Leo.Chimp.Domain/Leo.Chimp.Domain.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 1 addition & 6 deletions example/Leo.Chimp.Example/Leo.Chimp.Example.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Leo.Chimp.Domain\Leo.Chimp.Domain.csproj" />
</ItemGroup>
Expand Down
10 changes: 7 additions & 3 deletions example/Leo.Chimp.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Leo.Chimp.Example
Expand All @@ -17,8 +18,11 @@ public static void Main(string[] args)
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
public static IHostBuilder CreateWebHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
17 changes: 13 additions & 4 deletions example/Leo.Chimp.Example/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

Expand All @@ -26,21 +27,29 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddControllersWithViews();
services.AddChimp<ChimpDbContext>(
opt => opt.UseSqlServer("Server=10.0.0.99;Database=Chimp;Uid=sa;Pwd=Fuluerp123")
/*opt => opt.UseSqlServer("Server=10.0.0.99;Database=Chimp;Uid=sa;Pwd=Fuluerp123")*/
opt => opt.UseMySql("Server=192.168.5.5;Database=Chimp;Uid=root;Pwd='luodaoyi';SslMode=none")
);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseMvc();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
});
}
}
}
23 changes: 23 additions & 0 deletions sqlscript/sqllite/script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

-- ----------------------------
-- Table structure for school
-- ----------------------------
DROP TABLE IF EXISTS School;
CREATE TABLE School (
Id CHAR(32) PRIMARY KEY
NOT NULL,
Name VARCHAR (255)
);

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS Student;
CREATE TABLE Student (
Id CHAR(32) NOT NULL
PRIMARY KEY,
Name VARCHAR (255),
Age INT,
Birthday DATETIME,
SchoolId CHAR (32)
);
16 changes: 16 additions & 0 deletions src/Leo.Chimp/DapperAdapter/SqliteAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Leo.Chimp.DapperAdapter
{
public class SqliteAdapter : ISqlAdapter
{
public virtual string PagingBuild(ref PartedSql partedSql, object args, long skip, long take)
{
var pageSql = $"{partedSql.Raw} LIMIT {take} OFFSET {skip}";
return pageSql;
}

}
}
5 changes: 3 additions & 2 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,7 +32,7 @@ public TEntity GetById(object id)

return Entities.Find(id);
}
public Task<TEntity> GetByIdAsync(object id)
public ValueTask<TEntity> GetByIdAsync(object id)
{
if (id == null)
throw new ArgumentNullException("id");
Expand All @@ -56,7 +57,7 @@ public void Insert(IEnumerable<TEntity> entities)
Entities.AddRange(entities);
}

public Task InsertAsync(TEntity entity)
public ValueTask<EntityEntry<TEntity>> InsertAsync(TEntity entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
Expand Down
5 changes: 3 additions & 2 deletions src/Leo.Chimp/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Query;

namespace Leo.Chimp
Expand All @@ -30,7 +31,7 @@ public interface IRepository<TEntity> where TEntity : class
/// </summary>
/// <param name="id">主键</param>
/// <returns>Entity</returns>
Task<TEntity> GetByIdAsync(object id);
ValueTask<TEntity> GetByIdAsync(object id);

/// <summary>
/// Table Tracking
Expand Down Expand Up @@ -60,7 +61,7 @@ public interface IRepository<TEntity> where TEntity : class
/// 添加实体
/// </summary>
/// <param name="entity">Inserted entity</param>
Task InsertAsync(TEntity entity);
ValueTask<EntityEntry<TEntity>> InsertAsync(TEntity entity);

/// <summary>
/// InsertAsync
Expand Down
3 changes: 2 additions & 1 deletion src/Leo.Chimp/KingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class KingOptions
public enum DbType
{
MSSQL,
MYSQL
MYSQL,
SQLITE
}

}
21 changes: 11 additions & 10 deletions src/Leo.Chimp/Leo.Chimp.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageProjectUrl>https://github.com/longxianghui/chimp</PackageProjectUrl>
<TargetFramework>netstandard2.1</TargetFramework>
<PackageProjectUrl>https://github.com/luodaoyi/chimp</PackageProjectUrl>
<LangVersion>7.3</LangVersion>
<Version>2.1.2</Version>
<Authors>longxianghui</Authors>
<Version>3.0.0</Version>
<Authors>luodaoyi</Authors>
<Description>ef and dapper repository.</Description>
<PackageTags>repository;unit of work;ef;dapper;</PackageTags>
<RepositoryUrl>https://github.com/longxianghui/chimp</RepositoryUrl>
<RepositoryUrl>https://github.com/luodaoyi/chimp</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<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" />
<PackageReference Include="Dapper" Version="2.0.35" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="3.1.6" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.2" />
</ItemGroup>

</Project>
6 changes: 4 additions & 2 deletions src/Leo.Chimp/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ public async Task<PagedList<TEntity>> QueryPagedListAsync<TEntity>(int pageIndex
ISqlAdapter sqlAdapter = null;
if (_context.Database.IsMySql())
sqlAdapter = new MysqlAdapter();
if (_context.Database.IsSqlServer())
else if(_context.Database.IsSqlServer())
sqlAdapter = new SqlServerAdapter();
if (sqlAdapter == null)
else if(_context.Database.IsSqlite())
sqlAdapter = new SqliteAdapter();
else
throw new Exception("Unsupported database type");
pageSql = sqlAdapter.PagingBuild(ref partedSql, pageSqlArgs, (pageIndex - 1) * pageSize, pageSize);
var sqlCount = PagingUtil.GetCountSql(partedSql);
Expand Down
8 changes: 4 additions & 4 deletions test/Leo.Chimp.Test/ChimpDbContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace Leo.Chimp.Test

public class ChimpDbContextTest
{
private readonly DbType dbType;

private readonly IUnitOfWork _unitOfWork;
private readonly ISchoolRepository _schoolRepository;
public ChimpDbContextTest()
{
var services = new ServiceCollection();
services.AddChimp<ChimpDbContext>(opt =>
{
opt.UseMySql("server = 10.0.0.146;database=chimp;uid=root;password=123456;");
});
dbType = DbType.MYSQL;
InitChimpTestDb.Start(services, dbType);
var sp = services.BuildServiceProvider();
_unitOfWork = sp.GetRequiredService<IUnitOfWork>();
_schoolRepository = sp.GetRequiredService<ISchoolRepository>();
Expand Down
Loading

0 comments on commit 9f5df71

Please sign in to comment.