diff --git a/Examples/base_entity_net45/App.config b/Examples/base_entity_net45/App.config new file mode 100644 index 00000000..88fa4027 --- /dev/null +++ b/Examples/base_entity_net45/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Examples/base_entity_net45/BaseEntitySync.cs b/Examples/base_entity_net45/BaseEntitySync.cs new file mode 100644 index 00000000..112c26b1 --- /dev/null +++ b/Examples/base_entity_net45/BaseEntitySync.cs @@ -0,0 +1,153 @@ +using FreeSql; +using FreeSql.DataAnnotations; +using Newtonsoft.Json; +using System; +using System.Data; +using System.Diagnostics; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; + +[Table(DisableSyncStructure = true)] +public abstract class BaseEntity +{ + private static Lazy _ormLazy = new Lazy(() => + { + var orm = new FreeSqlBuilder() + .UseAutoSyncStructure(true) + .UseNoneCommandParameter(true) + .UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=5") + //.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=2") + //.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2") + //.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=2") + //.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2") + .Build(); + orm.Aop.CurdBefore += (s, e) => Trace.WriteLine(e.Sql + "\r\n"); + return orm; + }); + public static IFreeSql Orm => _ormLazy.Value; + + /// + /// 创建时间 + /// + public DateTime CreateTime { get; set; } + /// + /// 更新时间 + /// + public DateTime UpdateTime { get; set; } + /// + /// 逻辑删除 + /// + public bool IsDeleted { get; set; } +} + +[Table(DisableSyncStructure = true)] +public abstract class BaseEntity : BaseEntity where TEntity : class +{ + public static ISelect Select => Orm.Select() + .WhereCascade(a => (a as BaseEntity).IsDeleted == false); + public static ISelect Where(Expression> exp) => Select.Where(exp); + public static ISelect WhereIf(bool condition, Expression> exp) => Select.WhereIf(condition, exp); + + [JsonIgnore] + protected IBaseRepository Repository { get; set; } + + bool UpdateIsDeleted(bool value) + { + if (this.Repository == null) + return Orm.Update(this as TEntity) + .Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1; + + this.IsDeleted = value; + return this.Repository.Update(this as TEntity) == 1; + } + /// + /// 删除数据 + /// + /// + public virtual bool Delete() => this.UpdateIsDeleted(true); + /// + /// 恢复删除的数据 + /// + /// + public virtual bool Restore() => this.UpdateIsDeleted(false); + + /// + /// 附加实体,在更新数据时,只更新变化的部分 + /// + public TEntity Attach() + { + if (this.Repository == null) + this.Repository = Orm.GetRepository(); + + var item = this as TEntity; + this.Repository.Attach(item); + return item; + } + /// + /// 更新数据 + /// + /// + public virtual bool Update() + { + this.UpdateTime = DateTime.Now; + if (this.Repository == null) + return Orm.Update() + .SetSource(this as TEntity).ExecuteAffrows() == 1; + + return this.Repository.Update(this as TEntity) == 1; + } + /// + /// 插入数据 + /// + public virtual TEntity Insert() + { + this.CreateTime = DateTime.Now; + if (this.Repository == null) + this.Repository = Orm.GetRepository(); + + return this.Repository.Insert(this as TEntity); + } + + /// + /// 更新或插入 + /// + /// + public virtual TEntity Save() + { + this.UpdateTime = DateTime.Now; + if (this.Repository == null) + this.Repository = Orm.GetRepository(); + + return this.Repository.InsertOrUpdate(this as TEntity); + } +} + +[Table(DisableSyncStructure = true)] +public abstract class BaseEntity : BaseEntity 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)); + } + + /// + /// 主键 + /// + public virtual TKey Id { get; set; } + + /// + /// 根据主键值获取数据 + /// + /// + /// + public static TEntity Find(TKey id) + { + var item = Select.WhereDynamic(id).First(); + (item as BaseEntity)?.Attach(); + return item; + } +} \ No newline at end of file diff --git a/Examples/base_entity_net45/Program.cs b/Examples/base_entity_net45/Program.cs new file mode 100644 index 00000000..468b7804 --- /dev/null +++ b/Examples/base_entity_net45/Program.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace base_entity_net45 +{ + class Program + { + static void Main(string[] args) + { + } + } +} diff --git a/Examples/base_entity_net45/Properties/AssemblyInfo.cs b/Examples/base_entity_net45/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..6fa2e439 --- /dev/null +++ b/Examples/base_entity_net45/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("base_entity_net45")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("base_entity_net45")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("7e091544-ec38-4a41-a3be-bdc693070be7")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Examples/base_entity_net45/base_entity_net45.csproj b/Examples/base_entity_net45/base_entity_net45.csproj new file mode 100644 index 00000000..474dc221 --- /dev/null +++ b/Examples/base_entity_net45/base_entity_net45.csproj @@ -0,0 +1,73 @@ + + + + + Debug + AnyCPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7} + Exe + base_entity_net45 + base_entity_net45 + v4.5.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + {82885c27-23c8-4a6e-92cf-80fe61a041e1} + FreeSql.DbContext + + + {af9c50ec-6eb6-494b-9b3b-7edba6fd0ebb} + FreeSql + + + {559b6369-1868-4a06-a590-f80ba7b80a1b} + FreeSql.Provider.Sqlite + + + + + 12.0.2 + + + + \ No newline at end of file diff --git a/FreeSql.sln b/FreeSql.sln index 749a39d2..868aa88e 100644 --- a/FreeSql.sln +++ b/FreeSql.sln @@ -54,7 +54,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Tests.PerformanceTe EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.Tests.Provider.MySqlConnector", "FreeSql.Tests\FreeSql.Tests.Provider.MySqlConnector\FreeSql.Tests.Provider.MySqlConnector.csproj", "{0F45294A-34FF-4FB8-A046-20E09E3A4D5C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "base_entity", "Examples\base_entity\base_entity.csproj", "{D3A1869C-A8DE-46D0-8587-89EBBE3E4DD0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "base_entity", "Examples\base_entity\base_entity.csproj", "{D3A1869C-A8DE-46D0-8587-89EBBE3E4DD0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "base_entity_net45", "Examples\base_entity_net45\base_entity_net45.csproj", "{7E091544-EC38-4A41-A3BE-BDC693070BE7}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -318,6 +320,18 @@ Global {D3A1869C-A8DE-46D0-8587-89EBBE3E4DD0}.Release|x64.Build.0 = Release|Any CPU {D3A1869C-A8DE-46D0-8587-89EBBE3E4DD0}.Release|x86.ActiveCfg = Release|Any CPU {D3A1869C-A8DE-46D0-8587-89EBBE3E4DD0}.Release|x86.Build.0 = Release|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Debug|x64.ActiveCfg = Debug|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Debug|x64.Build.0 = Debug|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Debug|x86.ActiveCfg = Debug|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Debug|x86.Build.0 = Debug|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Release|Any CPU.Build.0 = Release|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Release|x64.ActiveCfg = Release|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Release|x64.Build.0 = Release|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Release|x86.ActiveCfg = Release|Any CPU + {7E091544-EC38-4A41-A3BE-BDC693070BE7}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -337,6 +351,7 @@ Global {690F89E0-A721-423F-8F5D-D262F73235EA} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B} {4C0973CB-BD49-4A5B-A6FE-EE0594BDD513} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B} {D3A1869C-A8DE-46D0-8587-89EBBE3E4DD0} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B} + {7E091544-EC38-4A41-A3BE-BDC693070BE7} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {089687FD-5D25-40AB-BA8A-A10D1E137F98}