mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
orm_vs add efcore
This commit is contained in:
parent
a0cebe229e
commit
62ff2bda05
@ -1,7 +1,9 @@
|
|||||||
using FreeSql.DataAnnotations;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -11,8 +13,8 @@ namespace orm_vs
|
|||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||||
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=20")
|
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=20")
|
||||||
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=20")
|
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=20")
|
||||||
.UseAutoSyncStructure(false)
|
.UseAutoSyncStructure(false)
|
||||||
.UseNoneCommandParameter(true)
|
.UseNoneCommandParameter(true)
|
||||||
//.UseConfigEntityFromDbFirst(true)
|
//.UseConfigEntityFromDbFirst(true)
|
||||||
@ -21,26 +23,35 @@ namespace orm_vs
|
|||||||
static SqlSugarClient sugar {
|
static SqlSugarClient sugar {
|
||||||
get => new SqlSugarClient(new ConnectionConfig() {
|
get => new SqlSugarClient(new ConnectionConfig() {
|
||||||
//不欺负,让连接池100个最小
|
//不欺负,让连接池100个最小
|
||||||
ConnectionString = "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=20;Max Pool Size=20",
|
//ConnectionString = "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=20;Max Pool Size=20",
|
||||||
DbType = DbType.SqlServer,
|
//DbType = DbType.SqlServer,
|
||||||
//ConnectionString = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Min Pool Size=20;Max Pool Size=20",
|
ConnectionString = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Min Pool Size=20;Max Pool Size=20",
|
||||||
//DbType = DbType.MySql,
|
DbType = DbType.MySql,
|
||||||
IsAutoCloseConnection = true,
|
IsAutoCloseConnection = true,
|
||||||
InitKeyType = InitKeyType.Attribute
|
InitKeyType = InitKeyType.Attribute
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class SongContext : DbContext {
|
||||||
|
public DbSet<Song> Songs { get; set; }
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
||||||
|
//optionsBuilder.UseSqlServer(@"Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=21;Max Pool Size=21");
|
||||||
|
optionsBuilder.UseMySql("Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Min Pool Size=21;Max Pool Size=21");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static StringBuilder sb = new StringBuilder();
|
static StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
static void Main(string[] args) {
|
static void Main(string[] args) {
|
||||||
|
|
||||||
//fsql.CodeFirst.SyncStructure(typeof(Song), typeof(Song_tag), typeof(Tag));
|
fsql.CodeFirst.SyncStructure(typeof(Song), typeof(Song_tag), typeof(Tag));
|
||||||
//sugar.CodeFirst.InitTables(typeof(Song), typeof(Song_tag), typeof(Tag));
|
//sugar.CodeFirst.InitTables(typeof(Song), typeof(Song_tag), typeof(Tag));
|
||||||
//sugar创建表失败:SqlSugar.SqlSugarException: Sequence contains no elements
|
//sugar创建表失败:SqlSugar.SqlSugarException: Sequence contains no elements
|
||||||
|
|
||||||
//测试前清空数据
|
//测试前清空数据
|
||||||
fsql.Delete<Song>().Where(a => a.Id > 0).ExecuteAffrows();
|
fsql.Delete<Song>().Where(a => a.Id > 0).ExecuteAffrows();
|
||||||
sugar.Deleteable<Song>().Where(a => a.Id > 0).ExecuteCommand();
|
sugar.Deleteable<Song>().Where(a => a.Id > 0).ExecuteCommand();
|
||||||
|
fsql.Ado.ExecuteNonQuery("delete from efcore_song");
|
||||||
|
|
||||||
Console.WriteLine("插入性能:");
|
Console.WriteLine("插入性能:");
|
||||||
Insert(1000, 1);
|
Insert(1000, 1);
|
||||||
@ -99,7 +110,16 @@ namespace orm_vs
|
|||||||
for (var a = 0; a < forTime; a++)
|
for (var a = 0; a < forTime; a++)
|
||||||
sugar.Queryable<Song>().Take(size).ToList();
|
sugar.Queryable<Song>().Take(size).ToList();
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
sb.AppendLine($"SqlSugar Select {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms\r\n");
|
sb.AppendLine($"SqlSugar Select {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms");
|
||||||
|
|
||||||
|
sw.Restart();
|
||||||
|
for (var a = 0; a < forTime; a++) {
|
||||||
|
using (var db = new SongContext()) {
|
||||||
|
db.Songs.Take(size).ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sw.Stop();
|
||||||
|
sb.AppendLine($"EFCore Select {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Insert(int forTime, int size) {
|
static void Insert(int forTime, int size) {
|
||||||
@ -108,7 +128,7 @@ namespace orm_vs
|
|||||||
Is_deleted = false,
|
Is_deleted = false,
|
||||||
Title = $"Insert_{a}",
|
Title = $"Insert_{a}",
|
||||||
Url = $"Url_{a}"
|
Url = $"Url_{a}"
|
||||||
}).ToArray();
|
});
|
||||||
|
|
||||||
//预热
|
//预热
|
||||||
fsql.Insert(songs.First()).ExecuteAffrows();
|
fsql.Insert(songs.First()).ExecuteAffrows();
|
||||||
@ -122,19 +142,37 @@ namespace orm_vs
|
|||||||
sb.AppendLine($"FreeSql Insert {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms");
|
sb.AppendLine($"FreeSql Insert {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms");
|
||||||
|
|
||||||
sw.Restart();
|
sw.Restart();
|
||||||
for (var a = 0; a < forTime; a++)
|
Exception sugarEx = null;
|
||||||
sugar.Insertable(songs).ExecuteCommand();
|
try {
|
||||||
|
for (var a = 0; a < forTime; a++)
|
||||||
|
sugar.Insertable(songs.ToArray()).ExecuteCommand();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
sugarEx = ex;
|
||||||
|
}
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
|
sb.AppendLine($"SqlSugar Insert {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms" + (sugarEx != null ? $"成绩无效,错误:{sugarEx.Message}" : ""));
|
||||||
|
|
||||||
sb.AppendLine($"SqlSugar Insert {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms\r\n");
|
sw.Restart();
|
||||||
|
for (var a = 0; a < forTime; a++) {
|
||||||
|
|
||||||
|
using (var db = new SongContext()) {
|
||||||
|
db.Songs.AddRange(songs.ToArray());
|
||||||
|
db.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sw.Stop();
|
||||||
|
sb.AppendLine($"EFCore Insert {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Table(Name = "freesql_song")]
|
[FreeSql.DataAnnotations.Table(Name = "freesql_song")]
|
||||||
[SugarTable("sugar_song")]
|
[SugarTable("sugar_song")]
|
||||||
|
[Table("efcore_song")]
|
||||||
public class Song {
|
public class Song {
|
||||||
[Column(IsIdentity = true)]
|
[FreeSql.DataAnnotations.Column(IsIdentity = true)]
|
||||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||||
|
[Key]
|
||||||
|
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public DateTime? Create_time { get; set; }
|
public DateTime? Create_time { get; set; }
|
||||||
public bool? Is_deleted { get; set; }
|
public bool? Is_deleted { get; set; }
|
||||||
@ -142,35 +180,45 @@ namespace orm_vs
|
|||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
|
|
||||||
[SugarColumn(IsIgnore = true)]
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
[NotMapped]
|
||||||
public virtual ICollection<Tag> Tags { get; set; }
|
public virtual ICollection<Tag> Tags { get; set; }
|
||||||
}
|
}
|
||||||
[Table(Name = "freesql_song_tag")]
|
[FreeSql.DataAnnotations.Table(Name = "freesql_song_tag")]
|
||||||
[SugarTable("sugar_song_tag")]
|
[SugarTable("sugar_song_tag")]
|
||||||
|
[Table("efcore_song_tag")]
|
||||||
public class Song_tag {
|
public class Song_tag {
|
||||||
public int Song_id { get; set; }
|
public int Song_id { get; set; }
|
||||||
[SugarColumn(IsIgnore = true)]
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
[NotMapped]
|
||||||
public virtual Song Song { get; set; }
|
public virtual Song Song { get; set; }
|
||||||
|
|
||||||
public int Tag_id { get; set; }
|
public int Tag_id { get; set; }
|
||||||
[SugarColumn(IsIgnore = true)]
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
[NotMapped]
|
||||||
public virtual Tag Tag { get; set; }
|
public virtual Tag Tag { get; set; }
|
||||||
}
|
}
|
||||||
[Table(Name = "freesql_tag")]
|
[FreeSql.DataAnnotations.Table(Name = "freesql_tag")]
|
||||||
[SugarTable("sugar_tag")]
|
[SugarTable("sugar_tag")]
|
||||||
|
[Table("efcore_tag")]
|
||||||
public class Tag {
|
public class Tag {
|
||||||
[Column(IsIdentity = true)]
|
[FreeSql.DataAnnotations.Column(IsIdentity = true)]
|
||||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||||
|
[Key]
|
||||||
|
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public int? Parent_id { get; set; }
|
public int? Parent_id { get; set; }
|
||||||
[SugarColumn(IsIgnore = true)]
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
[NotMapped]
|
||||||
public virtual Tag Parent { get; set; }
|
public virtual Tag Parent { get; set; }
|
||||||
|
|
||||||
public decimal? Ddd { get; set; }
|
public decimal? Ddd { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
[SugarColumn(IsIgnore = true)]
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
[NotMapped]
|
||||||
public virtual ICollection<Song> Songs { get; set; }
|
public virtual ICollection<Song> Songs { get; set; }
|
||||||
[SugarColumn(IsIgnore = true)]
|
[SugarColumn(IsIgnore = true)]
|
||||||
|
[NotMapped]
|
||||||
public virtual ICollection<Tag> Tags { get; set; }
|
public virtual ICollection<Tag> Tags { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.3" />
|
||||||
|
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.0" />
|
||||||
<PackageReference Include="sqlSugarCore" Version="4.9.9.3" />
|
<PackageReference Include="sqlSugarCore" Version="4.9.9.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
@ -13,4 +15,10 @@
|
|||||||
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="Microsoft.EntityFrameworkCore">
|
||||||
|
<HintPath>..\..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore\2.2.0\lib\netstandard2.0\Microsoft.EntityFrameworkCore.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user