v3.0.0
This commit is contained in:
橙子
2021-05-31 21:41:27 +08:00
parent 8aa1065b37
commit 2bd67778bf
127 changed files with 3895 additions and 8 deletions

View File

@@ -0,0 +1,113 @@
using CC.Yi.IDAL;
using CC.Yi.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace CC.Yi.DAL
{
public class BaseDal<T> : IBaseDal<T> where T : class, new()
{
public DbContext Db
{
get { return DbContentFactory.GetCurrentDbContent(); }
}
public IQueryable<T> GetEntities(Expression<Func<T, bool>> whereLambda)
{
return Db.Set<T>().Where(whereLambda).AsQueryable();
}
public IQueryable<T> GetAllEntities()
{
return Db.Set<T>().AsQueryable();
}
public int GetCount(Expression<Func<T, bool>> whereLambda)
{
return Db.Set<T>().Where(whereLambda).Count();
}
public IQueryable<IGrouping<S, T>> GetGroup<S>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> groupByLambda) //分组
{
return Db.Set<T>().Where(whereLambda).GroupBy<T, S>(groupByLambda).AsQueryable();
}
public IQueryable<T> GetPageEntities<S>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderByLambda, bool isAsc)
{
total = Db.Set<T>().Where(whereLambda).Count();
if (isAsc)
{
var pageData = Db.Set<T>().Where(whereLambda)
.OrderBy<T, S>(orderByLambda)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize).AsQueryable();
return pageData;
}
else
{
var pageData = Db.Set<T>().Where(whereLambda)
.OrderByDescending<T, S>(orderByLambda)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize).AsQueryable();
return pageData;
}
}
public T Add(T entity)
{
Db.Set<T>().Add(entity);
//Db.SaveChanges();
return entity;
}
public bool AddRange(IEnumerable<T> entities)
{
Db.Set<T>().AddRange(entities);
return true;
}
public bool Update(T entity)
{
//所有字段均修改
Db.Entry<T>(entity).State = EntityState.Modified;
//return Db.SaveChanges() > 0;
return true;
}
public bool Update(T entity, params string[] propertyNames)
{
EntityEntry entry = Db.Entry<T>(entity);
entry.State = EntityState.Unchanged;
foreach (var item in propertyNames)
{
entry.Property(item).IsModified = true;
}
return true;
}
public bool Delete(T entity)
{
Db.Entry<T>(entity).State = EntityState.Deleted;
//return Db.SaveChanges() > 0;
return true;
}
public bool Detete(int id)
{
var entity = Db.Set<T>().Find(id);//根据id找到实体
Db.Set<T>().Remove(entity);//由于这里先Find找到了实体所以这里可以用Remove标记该实体要移除删除。如果不是先Find到实体就需要用System.Data.Entity.EntityState.Deleted
return true;
}
public bool DeteteRange(IEnumerable<T> entity)
{
Db.Set<T>().RemoveRange(entity);
return true;
}
}
}

View File

@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CC.Yi.IDAL\CC.Yi.IDAL.csproj" />
<ProjectReference Include="..\CC.Yi.Model\CC.Yi.Model.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="T4DAL.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>T4DAL.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
<ItemGroup>
<Compile Update="T4DAL.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>T4DAL.tt</DependentUpon>
</Compile>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,58 @@
using CC.Yi.Model;
using Microsoft.EntityFrameworkCore;
using System.Collections.Concurrent;
using System.Threading;
namespace CC.Yi.DAL
{
public class DbContentFactory
{
private static DataContext Webcontext;
private static object myLock = new object();
public static void Initialize(DataContext webContext)
{
Monitor.Enter(myLock);
{
Webcontext = webContext;
}
}
public static DataContext GetCurrentDbContent()
{
DataContext db = CallContext.GetData("DbContext") as DataContext;
if (db == null)//线程在数据槽里面没有此上下文
{
db = Webcontext;
CallContext.SetData("DbContext", db);//放到数据槽中去,DbContext是keydb是value
}
try
{
Monitor.Exit(myLock);
}
catch
{
}
return db;
}
private static class CallContext
{
static ConcurrentDictionary<string, AsyncLocal<object>> state = new ConcurrentDictionary<string, AsyncLocal<object>>();
public static void SetData(string name, object data) =>
state.GetOrAdd(name, _ => new AsyncLocal<object>()).Value = data;
public static object GetData(string name) =>
state.TryGetValue(name, out AsyncLocal<object> data) ? data.Value : null;
}
}
}

View File

@@ -0,0 +1,12 @@
using CC.Yi.IDAL;
using CC.Yi.Model;
using System;
using System.Collections.Generic;
using System.Text;
namespace CC.Yi.DAL
{
public partial class studentDal : BaseDal<student>, IstudentDal
{
}
}

View File

@@ -0,0 +1,30 @@
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>
<#
string solutionsPath = Host.ResolveAssemblyReference("$(SolutionDir)");//获取解决方案路径
string txt;
StreamReader sr = new StreamReader(solutionsPath+@"\T4Model\T4Model.txt");
txt=sr.ReadToEnd();
sr.Close();
string[] ModelData= txt.Split(',');
#>
using CC.Yi.IDAL;
using CC.Yi.Model;
using System;
using System.Collections.Generic;
using System.Text;
namespace CC.Yi.DAL
{
<# foreach(string k in ModelData){
#>
public partial class <#=k #>Dal : BaseDal<<#=k #>>, I<#=k #>Dal
{
}
<# } #>
}