mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 18:52:50 +08:00
parent
f8e4e27113
commit
bdf32ea736
@ -1,4 +1,6 @@
|
||||
using FreeSql;
|
||||
#if netcore
|
||||
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Data;
|
||||
@ -143,3 +145,5 @@ namespace FreeSql
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,4 +1,6 @@
|
||||
using FreeSql;
|
||||
#if netcore
|
||||
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
@ -125,3 +127,5 @@ namespace FreeSql
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,4 +1,6 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
#if netcore
|
||||
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
@ -200,3 +202,5 @@ namespace FreeSql
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,4 +1,6 @@
|
||||
using FreeSql;
|
||||
#if netcore
|
||||
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -162,3 +164,5 @@ namespace FreeSql
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,8 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<TargetFrameworks>netcoreapp31;netcoreapp21;net4.0;</TargetFrameworks>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>BaseEntity 是一种极简单的 CodeFirst 开发方式,特别对单表或多表CRUD,利用继承节省了每个实体类的重复属性(创建时间、ID等字段),软件删除等功能,进行 crud 操作时不必时常考虑仓储的使用.</Description>
|
||||
@ -31,4 +31,12 @@
|
||||
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Net40\" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp31' OR '$(TargetFramework)' == 'netcoreapp21'">
|
||||
<DefineConstants>netcore</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
110
Extensions/FreeSql.Extensions.BaseEntity/Net40/BaseEntity.cs
Normal file
110
Extensions/FreeSql.Extensions.BaseEntity/Net40/BaseEntity.cs
Normal file
@ -0,0 +1,110 @@
|
||||
#if netcore
|
||||
#else
|
||||
|
||||
using FreeSql;
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql
|
||||
{
|
||||
/// <summary>
|
||||
/// 包括 CreateTime/UpdateTime/IsDeleted、CRUD 方法、以及 ID 主键定义 的实体基类
|
||||
/// <para></para>
|
||||
/// 当 TKey 为 int/long 时,Id 主键被设为自增值主键
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity<TEntity, TKey> : BaseEntity<TEntity> where TEntity : class
|
||||
{
|
||||
static BaseEntity()
|
||||
{
|
||||
var tkeyType = typeof(TKey)?.NullableTypeOrThis();
|
||||
if (tkeyType == typeof(int) || tkeyType == typeof(long))
|
||||
Orm.CodeFirst.ConfigEntity(typeof(TEntity),
|
||||
t => t.Property("Id").IsIdentity(true));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主键
|
||||
/// </summary>
|
||||
[Column(Position = 1)]
|
||||
public virtual TKey Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 根据主键值获取数据
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public static TEntity Find(TKey id)
|
||||
{
|
||||
var item = Select.WhereDynamic(id).First();
|
||||
(item as BaseEntity<TEntity>)?.Attach();
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 包括 CreateTime/UpdateTime/IsDeleted、以及 CRUD 异步和同步方法的实体基类
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity<TEntity> : BaseEntityReadOnly<TEntity> where TEntity : class
|
||||
{
|
||||
bool DeletedPrivate(bool value)
|
||||
{
|
||||
if (this.Repository == null)
|
||||
return Orm.Delete<TEntity>(this as TEntity)
|
||||
.ExecuteAffrows() == 1;
|
||||
|
||||
return this.Repository.Delete(this as TEntity) == 1;
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool Delete() => this.DeletedPrivate(true);
|
||||
|
||||
/// <summary>
|
||||
/// 更新数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool Update()
|
||||
{
|
||||
if (this.Repository == null)
|
||||
return Orm.Update<TEntity>()
|
||||
.SetSource(this as TEntity).ExecuteAffrows() == 1;
|
||||
|
||||
return this.Repository.Update(this as TEntity) == 1;
|
||||
}
|
||||
/// <summary>
|
||||
/// 插入数据
|
||||
/// </summary>
|
||||
public virtual TEntity Insert()
|
||||
{
|
||||
if (this.Repository == null)
|
||||
this.Repository = Orm.GetRepository<TEntity>();
|
||||
|
||||
return this.Repository.Insert(this as TEntity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新或插入
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual TEntity Save()
|
||||
{
|
||||
if (this.Repository == null)
|
||||
this.Repository = Orm.GetRepository<TEntity>();
|
||||
|
||||
return this.Repository.InsertOrUpdate(this as TEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,130 @@
|
||||
#if netcore
|
||||
#else
|
||||
|
||||
using FreeSql.DataAnnotations;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
|
||||
namespace FreeSql
|
||||
{
|
||||
/// <summary>
|
||||
/// 包括 CreateTime/UpdateTime/IsDeleted 的实体基类
|
||||
/// </summary>
|
||||
[Table(DisableSyncStructure = true)]
|
||||
public abstract class BaseEntity
|
||||
{
|
||||
static IFreeSql _ormPriv;
|
||||
/// <summary>
|
||||
/// 全局 IFreeSql orm 对象
|
||||
/// </summary>
|
||||
public static IFreeSql Orm => _ormPriv ?? throw new Exception(@"使用前请初始化 BaseEntity.Initialization(new FreeSqlBuilder()
|
||||
.UseAutoSyncStructure(true)
|
||||
.UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"")
|
||||
.Build());");
|
||||
|
||||
/// <summary>
|
||||
/// 初始化BaseEntity
|
||||
/// BaseEntity.Initialization(new FreeSqlBuilder()
|
||||
/// <para></para>
|
||||
/// .UseAutoSyncStructure(true)
|
||||
/// <para></para>
|
||||
/// .UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=5")
|
||||
/// <para></para>
|
||||
/// .Build());
|
||||
/// </summary>
|
||||
/// <param name="fsql">IFreeSql orm 对象</param>
|
||||
public static void Initialization(IFreeSql fsql)
|
||||
{
|
||||
_ormPriv = fsql;
|
||||
_ormPriv.Aop.CurdBefore += (s, e) => Trace.WriteLine(e.Sql + "\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class BaseEntityReadOnly<TEntity> : BaseEntity where TEntity : class
|
||||
{
|
||||
/// <summary>
|
||||
/// 查询数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static ISelect<TEntity> Select
|
||||
{
|
||||
get
|
||||
{
|
||||
var select = Orm.Select<TEntity>().TrackToList(TrackToList); //自动为每个元素 Attach;
|
||||
return select;
|
||||
}
|
||||
}
|
||||
|
||||
static void TrackToList(object list)
|
||||
{
|
||||
if (list == null) return;
|
||||
var ls = list as IList<TEntity>;
|
||||
if (ls == null)
|
||||
{
|
||||
var ie = list as IEnumerable;
|
||||
if (ie == null) return;
|
||||
var isFirst = true;
|
||||
foreach (var item in ie)
|
||||
{
|
||||
if (item == null) return;
|
||||
if (isFirst)
|
||||
{
|
||||
isFirst = false;
|
||||
var itemType = item.GetType();
|
||||
if (itemType == typeof(object)) return;
|
||||
if (itemType.FullName.StartsWith("Submission#")) itemType = itemType.BaseType;
|
||||
if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return;
|
||||
if (item is BaseEntity<TEntity> == false) return;
|
||||
}
|
||||
(item as BaseEntity<TEntity>)?.Attach();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (ls.Any() == false) return;
|
||||
if (ls.FirstOrDefault() is BaseEntity<TEntity> == false) return;
|
||||
if (Orm.CodeFirst.GetTableByEntity(typeof(TEntity))?.Primarys.Any() != true) return;
|
||||
foreach (var item in ls)
|
||||
(item as BaseEntity<TEntity>)?.Attach();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询条件,Where(a => a.Id > 10),支持导航对象查询,Where(a => a.Author.Email == "2881099@qq.com")
|
||||
/// </summary>
|
||||
/// <param name="exp">lambda表达式</param>
|
||||
/// <returns></returns>
|
||||
public static ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp);
|
||||
/// <summary>
|
||||
/// 查询条件,Where(true, a => a.Id > 10),支导航对象查询,Where(true, a => a.Author.Email == "2881099@qq.com")
|
||||
/// </summary>
|
||||
/// <param name="condition">true 时生效</param>
|
||||
/// <param name="exp">lambda表达式</param>
|
||||
/// <returns></returns>
|
||||
public static ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => Select.WhereIf(condition, exp);
|
||||
|
||||
/// <summary>
|
||||
/// 仓储对象
|
||||
/// </summary>
|
||||
protected IBaseRepository<TEntity> Repository { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 附加实体,在更新数据时,只更新变化的部分
|
||||
/// </summary>
|
||||
public TEntity Attach()
|
||||
{
|
||||
if (this.Repository == null)
|
||||
this.Repository = Orm.GetRepository<TEntity>();
|
||||
|
||||
var item = this as TEntity;
|
||||
this.Repository.Attach(item);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 扩展包实现,使用 FluentApi 方式配置实体模型,使用习惯接近 EFCore,方便过渡.</Description>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 扩展包,可实现实体类属性为对象时,以JSON形式映射存储.</Description>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 扩展包,可实现【延时加载】属性.</Description>
|
||||
|
@ -12,7 +12,7 @@
|
||||
<Description>使用 FreeSql 快速生成数据库的实体类,安装:dotnet tool install -g FreeSql.Generator</Description>
|
||||
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/2881099/FreeSql</RepositoryUrl>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<PackageTags>FreeSql DbFirst 实体生成器</PackageTags>
|
||||
</PropertyGroup>
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql is the ORM in .NetCore, .NetFramework, And Xamarin. It supports Mysql, Postgresql, SqlServer, Oracle, Sqlite, Odbc, 达梦, And Access</Description>
|
||||
|
@ -110,13 +110,6 @@
|
||||
清空状态数据
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
|
||||
<summary>
|
||||
根据 lambda 条件删除数据
|
||||
</summary>
|
||||
<param name="predicate"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.DbSet`1.Add(`0)">
|
||||
<summary>
|
||||
添加
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite/达梦/Access, and read/write separation、and split table.</Description>
|
||||
<PackageProjectUrl>https://github.com/2881099/FreeSql/wiki/Repository</PackageProjectUrl>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql is the ORM in .NetCore, .NetFramework, And Xamarin. It supports Mysql, Postgresql, SqlServer, Oracle, Sqlite, Odbc, 达梦, And Access</Description>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 数据库 Ms Access 实现</Description>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net452;net451;net45;net40</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 数据库实现,基于 MySql 5.6</Description>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 数据库实现,基于 MySql 5.6</Description>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 数据库 Odbc 实现,基于 {Oracle}、{SQL Server}、{MySQL ODBC 8.0 Unicode Driver}、{PostgreSQL Unicode(x64)}、{DM8 ODBC Driver} 专用访问实现,以及通用 Odbc 访问所有数据库</Description>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 数据库实现,基于 Oracle 11</Description>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net461;net452;net451;net45</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 数据库实现,基于 PostgreSQL 9.5</Description>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net451;net45;net40</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 数据库实现,基于 SqlServer 2005+,并根据版本适配分页方法:row_number 或 offset fetch next</Description>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<Version>1.0.0</Version>
|
||||
<Version>1.0.1</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql 数据库实现,基于 Sqlite 3.0,支持 .NetCore、.NetFramework、Xamarin</Description>
|
||||
|
@ -40,7 +40,7 @@ FreeSql 是功能强大的对象关系映射技术(O/RM),支持 .NETCore 2.1+
|
||||
- 要么[FreeSql.Connection.Extensions](https://github.com/2881099/FreeSql.Connection.Extensions),有点像Dapper的使用习惯;
|
||||
- 要么[FreeSql.BaseEntity](https://github.com/2881099/FreeSql/tree/master/Examples/base_entity),我求简单现在使用的这个;
|
||||
|
||||
> 即将推出 [FluentApi 实体模型与 EfCore 90% 相似的扩展包](https://github.com/2881099/FreeSql/tree/master/Extensions/FreeSql.Extensions.EfCoreFluentApi);
|
||||
> [FluentApi 与 EfCore 90% 相似的扩展包](https://github.com/2881099/FreeSql/tree/master/Extensions/FreeSql.Extensions.EfCoreFluentApi);
|
||||
|
||||
> 学习项目
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user