mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
## v0.4.12
- 增加 .First()/.FirstAsync() 指定字段查询的重载方法 #26; - 调整 FreeSql.Repository 直接引用 FreeSql.DbContext 内的仓储实现; - 移动 FreeSql.Repository 至 FreeSql.DbContext; - 补充 单独针对 MySql 枚举类型的单元测试;
This commit is contained in:
@ -1,214 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using FreeSql;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace dbcontext_01.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class ValuesController : ControllerBase
|
||||
{
|
||||
|
||||
IFreeSql _orm;
|
||||
public ValuesController(SongContext songContext,
|
||||
IFreeSql orm1, IFreeSql orm2,
|
||||
IFreeSql<long> orm3
|
||||
) {
|
||||
|
||||
_orm = orm1;
|
||||
|
||||
}
|
||||
|
||||
// GET api/values
|
||||
[HttpGet]
|
||||
async public Task<string> Get()
|
||||
{
|
||||
|
||||
long id = 0;
|
||||
|
||||
try {
|
||||
|
||||
var repos2Song = _orm.GetRepository<Song, int>();
|
||||
repos2Song.Where(a => a.Id > 10).ToList();
|
||||
//查询结果,进入 states
|
||||
|
||||
var song = new Song { };
|
||||
repos2Song.Insert(song);
|
||||
id = song.Id;
|
||||
|
||||
var adds = Enumerable.Range(0, 100)
|
||||
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
|
||||
.ToList();
|
||||
//创建一堆无主键值
|
||||
|
||||
repos2Song.Insert(adds);
|
||||
|
||||
for (var a = 0; a < 10; a++)
|
||||
adds[a].Title = "dkdkdkdk" + a;
|
||||
|
||||
repos2Song.Update(adds);
|
||||
//批量修改
|
||||
|
||||
repos2Song.Delete(adds.Skip(10).Take(20).ToList());
|
||||
//批量删除,10-20 元素的主键值会被清除
|
||||
|
||||
adds.Last().Url = "skldfjlksdjglkjjcccc";
|
||||
repos2Song.Update(adds.Last());
|
||||
|
||||
adds.First().Url = "skldfjlksdjglkjjcccc";
|
||||
repos2Song.Update(adds.First());
|
||||
|
||||
|
||||
using (var ctx = new SongContext()) {
|
||||
|
||||
ctx.Songs.Select.Where(a => a.Id > 10).ToList();
|
||||
//查询结果,进入 states
|
||||
|
||||
song = new Song { };
|
||||
//可插入的 song
|
||||
|
||||
ctx.Songs.Add(song);
|
||||
id = song.Id;
|
||||
//因有自增类型,立即开启事务执行SQL,返回自增值
|
||||
|
||||
adds = Enumerable.Range(0, 100)
|
||||
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
|
||||
.ToList();
|
||||
//创建一堆无主键值
|
||||
|
||||
ctx.Songs.AddRange(adds);
|
||||
//立即执行,将自增值赋给 adds 所有元素,因为有自增类型,如果其他类型,指定传入主键值,不会立即执行
|
||||
|
||||
for (var a = 0; a < 10; a++)
|
||||
adds[a].Title = "dkdkdkdk" + a;
|
||||
|
||||
ctx.Songs.UpdateRange(adds);
|
||||
//批量修改,进入队列
|
||||
|
||||
ctx.Songs.RemoveRange(adds.Skip(10).Take(20).ToList());
|
||||
//批量删除,进入队列,完成时 10-20 元素的主键值会被清除
|
||||
|
||||
//ctx.Songs.Update(adds.First());
|
||||
|
||||
adds.Last().Url = "skldfjlksdjglkjjcccc";
|
||||
ctx.Songs.Update(adds.Last());
|
||||
|
||||
adds.First().Url = "skldfjlksdjglkjjcccc";
|
||||
ctx.Songs.Update(adds.First());
|
||||
|
||||
//单条修改 urls 的值,进入队列
|
||||
|
||||
//throw new Exception("回滚");
|
||||
|
||||
//ctx.Songs.Select.First();
|
||||
//这里做一个查询,会立即打包【执行队列】,避免没有提交的数据,影响查询结果
|
||||
|
||||
ctx.SaveChanges();
|
||||
//打包【执行队列】,提交事务
|
||||
}
|
||||
|
||||
using (var uow = _orm.CreateUnitOfWork()) {
|
||||
|
||||
var reposSong = uow.GetRepository<Song, int>();
|
||||
reposSong.Where(a => a.Id > 10).ToList();
|
||||
//查询结果,进入 states
|
||||
|
||||
song = new Song { };
|
||||
reposSong.Insert(song);
|
||||
id = song.Id;
|
||||
|
||||
adds = Enumerable.Range(0, 100)
|
||||
.Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
|
||||
.ToList();
|
||||
//创建一堆无主键值
|
||||
|
||||
reposSong.Insert(adds);
|
||||
|
||||
for (var a = 0; a < 10; a++)
|
||||
adds[a].Title = "dkdkdkdk" + a;
|
||||
|
||||
reposSong.Update(adds);
|
||||
//批量修改
|
||||
|
||||
reposSong.Delete(adds.Skip(10).Take(20).ToList());
|
||||
//批量删除,10-20 元素的主键值会被清除
|
||||
|
||||
adds.Last().Url = "skldfjlksdjglkjjcccc";
|
||||
reposSong.Update(adds.Last());
|
||||
|
||||
adds.First().Url = "skldfjlksdjglkjjcccc";
|
||||
reposSong.Update(adds.First());
|
||||
|
||||
uow.Commit();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//using (var ctx = new SongContext()) {
|
||||
|
||||
// var song = new Song { };
|
||||
// await ctx.Songs.AddAsync(song);
|
||||
// id = song.Id;
|
||||
|
||||
// var adds = Enumerable.Range(0, 100)
|
||||
// .Select(a => new Song { Create_time = DateTime.Now, Is_deleted = false, Title = "xxxx" + a, Url = "url222" })
|
||||
// .ToList();
|
||||
// await ctx.Songs.AddRangeAsync(adds);
|
||||
|
||||
// for (var a = 0; a < adds.Count; a++)
|
||||
// adds[a].Title = "dkdkdkdk" + a;
|
||||
|
||||
// ctx.Songs.UpdateRange(adds);
|
||||
|
||||
// ctx.Songs.RemoveRange(adds.Skip(10).Take(20).ToList());
|
||||
|
||||
// //ctx.Songs.Update(adds.First());
|
||||
|
||||
// adds.Last().Url = "skldfjlksdjglkjjcccc";
|
||||
// ctx.Songs.Update(adds.Last());
|
||||
|
||||
// //throw new Exception("回滚");
|
||||
|
||||
// await ctx.SaveChangesAsync();
|
||||
//}
|
||||
} catch {
|
||||
var item = await _orm.Select<Song>().Where(a => a.Id == id).FirstAsync();
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
var item22 = await _orm.Select<Song>().Where(a => a.Id == id).FirstAsync();
|
||||
var item33 = await _orm.Select<Song>().Where(a => a.Id > id).ToListAsync();
|
||||
|
||||
return item22.Id.ToString();
|
||||
}
|
||||
|
||||
// GET api/values/5
|
||||
[HttpGet("{id}")]
|
||||
public ActionResult<string> Get(int id)
|
||||
{
|
||||
return "value";
|
||||
}
|
||||
|
||||
// POST api/values
|
||||
[HttpPost]
|
||||
public void Post([FromBody] string value)
|
||||
{
|
||||
}
|
||||
|
||||
// PUT api/values/5
|
||||
[HttpPut("{id}")]
|
||||
public void Put(int id, [FromBody] string value)
|
||||
{
|
||||
}
|
||||
|
||||
// DELETE api/values/5
|
||||
[HttpDelete("{id}")]
|
||||
public void Delete(int id)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace dbcontext_01 {
|
||||
|
||||
public class SongContext : DbContext {
|
||||
|
||||
public DbSet<Song> Songs { get; set; }
|
||||
public DbSet<Tag> Tags { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder builder) {
|
||||
builder.UseFreeSql(dbcontext_01.Startup.Fsql);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class Song {
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public DateTime? Create_time { get; set; }
|
||||
public bool? Is_deleted { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Url { get; set; }
|
||||
|
||||
public virtual ICollection<Tag> Tags { get; set; }
|
||||
|
||||
[Column(IsVersion = true)]
|
||||
public long versionRow { get; set; }
|
||||
}
|
||||
public class Song_tag {
|
||||
public int Song_id { get; set; }
|
||||
public virtual Song Song { get; set; }
|
||||
|
||||
public int Tag_id { get; set; }
|
||||
public virtual Tag Tag { get; set; }
|
||||
}
|
||||
|
||||
public class Tag {
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public int? Parent_id { get; set; }
|
||||
public virtual Tag Parent { get; set; }
|
||||
|
||||
public decimal? Ddd { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
public virtual ICollection<Song> Songs { get; set; }
|
||||
public virtual ICollection<Tag> Tags { get; set; }
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Threading.Tasks;
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace dbcontext_01
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
|
||||
public class Song {
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string BigNumber { get; set; }
|
||||
|
||||
[Column(IsVersion = true)]//使用简单
|
||||
public long versionRow { get; set; }
|
||||
}
|
||||
|
||||
public class SongContext : DbContext {
|
||||
|
||||
public DbSet<Song> Songs { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder builder) {
|
||||
builder.UseFreeSql(fsql);
|
||||
}
|
||||
}
|
||||
static IFreeSql fsql;
|
||||
public static void Main(string[] args) {
|
||||
fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\dd2.db;Pooling=true;Max Pool Size=10")
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseLazyLoading(true)
|
||||
.UseNoneCommandParameter(true)
|
||||
|
||||
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText))
|
||||
.Build();
|
||||
|
||||
using (var ctx = new SongContext()) {
|
||||
var song = new Song { BigNumber = "1000000000000000000" };
|
||||
ctx.Songs.Add(song);
|
||||
|
||||
ctx.Songs.Update(song);
|
||||
|
||||
song.BigNumber = (BigInteger.Parse(song.BigNumber) + 1).ToString();
|
||||
ctx.Songs.Update(song);
|
||||
|
||||
ctx.SaveChanges();
|
||||
}
|
||||
|
||||
CreateWebHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseStartup<Startup>();
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
{
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:53030/",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"dbcontext_01": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "http://localhost:53031/"
|
||||
}
|
||||
}
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Swashbuckle.AspNetCore.Swagger;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace dbcontext_01
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
|
||||
{
|
||||
Configuration = configuration;
|
||||
|
||||
Fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Pooling=true;Max Pool Size=10")
|
||||
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=10")
|
||||
.UseLogger(loggerFactory.CreateLogger<IFreeSql>())
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseLazyLoading(true)
|
||||
.UseNoneCommandParameter(true)
|
||||
|
||||
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText),
|
||||
(cmd, log) => Trace.WriteLine(log)
|
||||
)
|
||||
.Build();
|
||||
|
||||
Fsql2 = new FreeSql.FreeSqlBuilder()
|
||||
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document222.db;Pooling=true;Max Pool Size=10")
|
||||
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=10")
|
||||
.UseLogger(loggerFactory.CreateLogger<IFreeSql>())
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseLazyLoading(true)
|
||||
|
||||
.UseMonitorCommand(cmd => Trace.WriteLine(cmd.CommandText),
|
||||
(cmd, log) => Trace.WriteLine(log)
|
||||
)
|
||||
.Build<long>();
|
||||
}
|
||||
|
||||
enum MySql { }
|
||||
enum PgSql { }
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
public static IFreeSql Fsql { get; private set; }
|
||||
public static IFreeSql<long> Fsql2 { get; private set; }
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddMvc();
|
||||
services.AddSwaggerGen(options => {
|
||||
options.SwaggerDoc("v1", new Info {
|
||||
Version = "v1",
|
||||
Title = "FreeSql.DbContext API"
|
||||
});
|
||||
//options.IncludeXmlComments(xmlPath);
|
||||
});
|
||||
|
||||
|
||||
|
||||
services.AddSingleton<IFreeSql>(Fsql);
|
||||
services.AddSingleton<IFreeSql<long>>(Fsql2);
|
||||
services.AddFreeDbContext<SongContext>(options => options.UseFreeSql(Fsql));
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
Console.OutputEncoding = Encoding.GetEncoding("GB2312");
|
||||
Console.InputEncoding = Encoding.GetEncoding("GB2312");
|
||||
|
||||
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
||||
loggerFactory.AddDebug();
|
||||
|
||||
app.UseHttpMethodOverride(new HttpMethodOverrideOptions { FormFieldName = "X-Http-Method-Override" });
|
||||
app.UseDeveloperExceptionPage();
|
||||
app.UseMvc();
|
||||
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c => {
|
||||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "FreeSql.RESTful API V1");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
|
||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user