diff --git a/Examples/repository_01/Controllers/SongController.cs b/Examples/repository_01/Controllers/SongController.cs index 20b0d825..36660b75 100644 --- a/Examples/repository_01/Controllers/SongController.cs +++ b/Examples/repository_01/Controllers/SongController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using repository_01.Repositorys; using restful.Entitys; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -15,6 +16,12 @@ namespace restful.Controllers { public SongsController(IFreeSql fsql) { _songRepository = new SongRepository(fsql); + + //test code + var curd1 = fsql.GetRepository(); + var curd2 = fsql.GetRepository(); + var curd3 = fsql.GetRepository(); + var curd4 = fsql.GetGuidRepository(); } [HttpGet] diff --git a/FreeSql.Repository/BaseRepository.cs b/FreeSql.Repository/BaseRepository.cs index ce2fa027..de5fd837 100644 --- a/FreeSql.Repository/BaseRepository.cs +++ b/FreeSql.Repository/BaseRepository.cs @@ -40,7 +40,7 @@ namespace FreeSql { } } - public virtual List Insert(List entity) { + public virtual List Insert(IEnumerable entity) { switch (_fsql.Ado.DataType) { case DataType.SqlServer: case DataType.PostgreSQL: @@ -66,7 +66,7 @@ namespace FreeSql { } } - public virtual Task> InsertAsync(List entity) { + public virtual Task> InsertAsync(IEnumerable entity) { switch (_fsql.Ado.DataType) { case DataType.SqlServer: case DataType.PostgreSQL: diff --git a/FreeSql.Repository/FreeSql.Repository.csproj b/FreeSql.Repository/FreeSql.Repository.csproj index 3c29684a..903f7229 100644 --- a/FreeSql.Repository/FreeSql.Repository.csproj +++ b/FreeSql.Repository/FreeSql.Repository.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 0.1.5 + 0.1.5.3 YeXiangQin FreeSql 通用仓库层现实,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。 https://github.com/2881099/FreeSql diff --git a/FreeSql.Repository/GuidRepository.cs b/FreeSql.Repository/GuidRepository.cs index 24b40492..e2e30ebd 100644 --- a/FreeSql.Repository/GuidRepository.cs +++ b/FreeSql.Repository/GuidRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,14 +12,14 @@ namespace FreeSql { public GuidRepository(IFreeSql fsql) : base(fsql) { } - public override List Insert(List entity) { + public override List Insert(IEnumerable entity) { _fsql.Insert().AppendData(entity).ExecuteAffrows(); - return entity; + return entity.ToList(); } - async public override Task> InsertAsync(List entity) { + async public override Task> InsertAsync(IEnumerable entity) { await _fsql.Insert().AppendData(entity).ExecuteAffrowsAsync(); - return entity; + return entity.ToList(); } public override TEntity Insert(TEntity entity) { diff --git a/FreeSql.Repository/IBasicRepository.cs b/FreeSql.Repository/IBasicRepository.cs index db2f0a17..63db1f7c 100644 --- a/FreeSql.Repository/IBasicRepository.cs +++ b/FreeSql.Repository/IBasicRepository.cs @@ -6,11 +6,11 @@ namespace FreeSql { where TEntity : class { TEntity Insert(TEntity entity); - List Insert(List entity); + List Insert(IEnumerable entity); Task InsertAsync(TEntity entity); - Task> InsertAsync(List entity); + Task> InsertAsync(IEnumerable entity); int Update(TEntity entity); diff --git a/FreeSql.Repository/IFreeSqlExtenssions.cs b/FreeSql.Repository/IFreeSqlExtenssions.cs index 033f1b16..36edc276 100644 --- a/FreeSql.Repository/IFreeSqlExtenssions.cs +++ b/FreeSql.Repository/IFreeSqlExtenssions.cs @@ -1,6 +1,7 @@ using FreeSql; using System; using System.Collections.Generic; +using System.Collections.Concurrent; using System.Text; public static class IFreeSqlExtenssions { @@ -12,10 +13,12 @@ public static class IFreeSqlExtenssions { /// /// /// - public static IRepository GetRepository(this IFreeSql that) where TEntity : class { + public static DefaultRepository GetRepository(this IFreeSql that) where TEntity : class { - return new DefaultRepository(that); + return dicGetRepository.GetOrAdd(typeof(TEntity), type1 => new ConcurrentDictionary()) + .GetOrAdd(typeof(TKey), type2 => new DefaultRepository(that)) as DefaultRepository; } + static ConcurrentDictionary> dicGetRepository = new ConcurrentDictionary>(); /// /// 返回仓库类,适用 Insert 方法无须返回插入的数据 @@ -23,8 +26,9 @@ public static class IFreeSqlExtenssions { /// /// /// - public static IRepository GetGuidRepository(this IFreeSql that) where TEntity : class { + public static GuidRepository GetGuidRepository(this IFreeSql that) where TEntity : class { - return new GuidRepository(that); + return dicGetGuidRepository.GetOrAdd(typeof(TEntity), type1 => new GuidRepository(that)) as GuidRepository; } + static ConcurrentDictionary dicGetGuidRepository = new ConcurrentDictionary(); } \ No newline at end of file diff --git a/FreeSql.Tests/FreeSql.Tests.csproj b/FreeSql.Tests/FreeSql.Tests.csproj index 64205046..8924c76b 100644 --- a/FreeSql.Tests/FreeSql.Tests.csproj +++ b/FreeSql.Tests/FreeSql.Tests.csproj @@ -13,6 +13,7 @@ + diff --git a/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs b/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs index c38cac49..5cac46fc 100644 --- a/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs +++ b/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs @@ -9,8 +9,55 @@ using Xunit; namespace FreeSql.Tests.Sqlite { public class SqliteCodeFirstTest { + + class Topic { + public Guid Id { get; set; } + public string Title { get; set; } + public string Content { get; set; } + public DateTime CreateTime { get; set; } + } + [Table(Name = "xxxtb.Comment")] + class Comment { + public Guid Id { get; set; } + public Guid TopicId { get; set; } + public Topic Topic { get; set; } + public string Nickname { get; set; } + public string Content { get; set; } + public DateTime CreateTime { get; set; } + } + + [Fact] public void AddField() { + + //һ FreeSql.Repository չdotnet add package FreeSql.Repository + var topicRepository = g.sqlite.GetGuidRepository(); + var commentRepository = g.sqlite.GetGuidRepository(); + + //Ӳ + var topicId = FreeUtil.NewMongodbId(); + topicRepository.Insert(new Topic { + Id = FreeUtil.NewMongodbId(), + Title = "±1", + Content = "1", + CreateTime = DateTime.Now + }); + + //10 + var comments = Enumerable.Range(0, 10).Select(a => new Comment { + Id = FreeUtil.NewMongodbId(), + TopicId = topicId, + Nickname = $"dz{a}", + Content = $"{a}", + CreateTime = DateTime.Now + }); + var affrows = commentRepository.Insert(comments); + + var find = commentRepository.Select.Where(a => a.Topic.Title == "±1").ToList(); + + + + var sql = g.sqlite.CodeFirst.GetComparisonDDLStatements(); var id = g.sqlite.Insert().AppendData(new TopicAddField { }).ExecuteIdentity(); diff --git a/readme.md b/readme.md index 73a2e243..7d137ffb 100644 --- a/readme.md +++ b/readme.md @@ -34,6 +34,7 @@ IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseAutoSyncStructure(true) //自动同步实体结构到数据库 .UseSyncStructureToLower(true) //转小写同步结构 + .UseSyncStructureToUpper(true) //转大写同步结构 .UseLazyLoading(true) //延时加载导航属性对象,导航属性需要声明 virtual .Build();