mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	add Examples: base_entity_net45
This commit is contained in:
		
							
								
								
									
										6
									
								
								Examples/base_entity_net45/App.config
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								Examples/base_entity_net45/App.config
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
<?xml version="1.0" encoding="utf-8" ?>
 | 
			
		||||
<configuration>
 | 
			
		||||
    <startup> 
 | 
			
		||||
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
 | 
			
		||||
    </startup>
 | 
			
		||||
</configuration>
 | 
			
		||||
							
								
								
									
										153
									
								
								Examples/base_entity_net45/BaseEntitySync.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										153
									
								
								Examples/base_entity_net45/BaseEntitySync.cs
									
									
									
									
									
										Normal file
									
								
							@@ -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<IFreeSql> _ormLazy = new Lazy<IFreeSql>(() =>
 | 
			
		||||
    {
 | 
			
		||||
        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;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// 创建时间
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public DateTime CreateTime { get; set; }
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// 更新时间
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public DateTime UpdateTime { get; set; }
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// 逻辑删除
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public bool IsDeleted { get; set; }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
[Table(DisableSyncStructure = true)]
 | 
			
		||||
public abstract class BaseEntity<TEntity> : BaseEntity where TEntity : class
 | 
			
		||||
{
 | 
			
		||||
    public static ISelect<TEntity> Select => Orm.Select<TEntity>()
 | 
			
		||||
        .WhereCascade(a => (a as BaseEntity<TEntity>).IsDeleted == false);
 | 
			
		||||
    public static ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp);
 | 
			
		||||
    public static ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => Select.WhereIf(condition, exp);
 | 
			
		||||
 | 
			
		||||
    [JsonIgnore]
 | 
			
		||||
    protected IBaseRepository<TEntity> Repository { get; set; }
 | 
			
		||||
 | 
			
		||||
    bool UpdateIsDeleted(bool value)
 | 
			
		||||
    {
 | 
			
		||||
        if (this.Repository == null)
 | 
			
		||||
            return Orm.Update<TEntity>(this as TEntity)
 | 
			
		||||
                .Set(a => (a as BaseEntity<TEntity>).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1;
 | 
			
		||||
 | 
			
		||||
        this.IsDeleted = value;
 | 
			
		||||
        return this.Repository.Update(this as TEntity) == 1;
 | 
			
		||||
    }
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// 删除数据
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <returns></returns>
 | 
			
		||||
    public virtual bool Delete() => this.UpdateIsDeleted(true);
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// 恢复删除的数据
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <returns></returns>
 | 
			
		||||
    public virtual bool Restore() => this.UpdateIsDeleted(false);
 | 
			
		||||
 | 
			
		||||
    /// <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;
 | 
			
		||||
    }
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// 更新数据
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <returns></returns>
 | 
			
		||||
    public virtual bool Update()
 | 
			
		||||
    {
 | 
			
		||||
        this.UpdateTime = DateTime.Now;
 | 
			
		||||
        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()
 | 
			
		||||
    {
 | 
			
		||||
        this.CreateTime = DateTime.Now;
 | 
			
		||||
        if (this.Repository == null)
 | 
			
		||||
            this.Repository = Orm.GetRepository<TEntity>();
 | 
			
		||||
 | 
			
		||||
        return this.Repository.Insert(this as TEntity);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// 更新或插入
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <returns></returns>
 | 
			
		||||
    public virtual TEntity Save()
 | 
			
		||||
    {
 | 
			
		||||
        this.UpdateTime = DateTime.Now;
 | 
			
		||||
        if (this.Repository == null)
 | 
			
		||||
            this.Repository = Orm.GetRepository<TEntity>();
 | 
			
		||||
 | 
			
		||||
        return this.Repository.InsertOrUpdate(this as TEntity);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
[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>
 | 
			
		||||
    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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								Examples/base_entity_net45/Program.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								Examples/base_entity_net45/Program.cs
									
									
									
									
									
										Normal file
									
								
							@@ -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)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								Examples/base_entity_net45/Properties/AssemblyInfo.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								Examples/base_entity_net45/Properties/AssemblyInfo.cs
									
									
									
									
									
										Normal file
									
								
							@@ -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")]
 | 
			
		||||
							
								
								
									
										73
									
								
								Examples/base_entity_net45/base_entity_net45.csproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								Examples/base_entity_net45/base_entity_net45.csproj
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,73 @@
 | 
			
		||||
<?xml version="1.0" encoding="utf-8"?>
 | 
			
		||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 | 
			
		||||
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
 | 
			
		||||
  <PropertyGroup>
 | 
			
		||||
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
 | 
			
		||||
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
 | 
			
		||||
    <ProjectGuid>{7E091544-EC38-4A41-A3BE-BDC693070BE7}</ProjectGuid>
 | 
			
		||||
    <OutputType>Exe</OutputType>
 | 
			
		||||
    <RootNamespace>base_entity_net45</RootNamespace>
 | 
			
		||||
    <AssemblyName>base_entity_net45</AssemblyName>
 | 
			
		||||
    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
 | 
			
		||||
    <FileAlignment>512</FileAlignment>
 | 
			
		||||
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
 | 
			
		||||
    <Deterministic>true</Deterministic>
 | 
			
		||||
  </PropertyGroup>
 | 
			
		||||
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
 | 
			
		||||
    <PlatformTarget>AnyCPU</PlatformTarget>
 | 
			
		||||
    <DebugSymbols>true</DebugSymbols>
 | 
			
		||||
    <DebugType>full</DebugType>
 | 
			
		||||
    <Optimize>false</Optimize>
 | 
			
		||||
    <OutputPath>bin\Debug\</OutputPath>
 | 
			
		||||
    <DefineConstants>DEBUG;TRACE</DefineConstants>
 | 
			
		||||
    <ErrorReport>prompt</ErrorReport>
 | 
			
		||||
    <WarningLevel>4</WarningLevel>
 | 
			
		||||
  </PropertyGroup>
 | 
			
		||||
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
 | 
			
		||||
    <PlatformTarget>AnyCPU</PlatformTarget>
 | 
			
		||||
    <DebugType>pdbonly</DebugType>
 | 
			
		||||
    <Optimize>true</Optimize>
 | 
			
		||||
    <OutputPath>bin\Release\</OutputPath>
 | 
			
		||||
    <DefineConstants>TRACE</DefineConstants>
 | 
			
		||||
    <ErrorReport>prompt</ErrorReport>
 | 
			
		||||
    <WarningLevel>4</WarningLevel>
 | 
			
		||||
  </PropertyGroup>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <Reference Include="System" />
 | 
			
		||||
    <Reference Include="System.Core" />
 | 
			
		||||
    <Reference Include="System.Xml.Linq" />
 | 
			
		||||
    <Reference Include="System.Data.DataSetExtensions" />
 | 
			
		||||
    <Reference Include="Microsoft.CSharp" />
 | 
			
		||||
    <Reference Include="System.Data" />
 | 
			
		||||
    <Reference Include="System.Net.Http" />
 | 
			
		||||
    <Reference Include="System.Xml" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <Compile Include="BaseEntitySync.cs" />
 | 
			
		||||
    <Compile Include="Program.cs" />
 | 
			
		||||
    <Compile Include="Properties\AssemblyInfo.cs" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <None Include="App.config" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj">
 | 
			
		||||
      <Project>{82885c27-23c8-4a6e-92cf-80fe61a041e1}</Project>
 | 
			
		||||
      <Name>FreeSql.DbContext</Name>
 | 
			
		||||
    </ProjectReference>
 | 
			
		||||
    <ProjectReference Include="..\..\FreeSql\FreeSql.csproj">
 | 
			
		||||
      <Project>{af9c50ec-6eb6-494b-9b3b-7edba6fd0ebb}</Project>
 | 
			
		||||
      <Name>FreeSql</Name>
 | 
			
		||||
    </ProjectReference>
 | 
			
		||||
    <ProjectReference Include="..\..\Providers\FreeSql.Provider.Sqlite\FreeSql.Provider.Sqlite.csproj">
 | 
			
		||||
      <Project>{559b6369-1868-4a06-a590-f80ba7b80a1b}</Project>
 | 
			
		||||
      <Name>FreeSql.Provider.Sqlite</Name>
 | 
			
		||||
    </ProjectReference>
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <PackageReference Include="Newtonsoft.Json">
 | 
			
		||||
      <Version>12.0.2</Version>
 | 
			
		||||
    </PackageReference>
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 | 
			
		||||
</Project>
 | 
			
		||||
		Reference in New Issue
	
	Block a user