v1.0.0
This commit is contained in:
86
CC.Yi.DAL/BaseDal.cs
Normal file
86
CC.Yi.DAL/BaseDal.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using CC.Yi.IDAL;
|
||||
using CC.Yi.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace CC.Yi.DAL
|
||||
{
|
||||
public class BaseDal<T> : IBaseDal<T> where T : class, new()
|
||||
{
|
||||
public DataContext Db;
|
||||
public BaseDal(DataContext _Db)
|
||||
{
|
||||
Db = _Db;
|
||||
}
|
||||
public IQueryable<T> GetEntities(Expression<Func<T, bool>> whereLambda)
|
||||
{
|
||||
return Db.Set<T>().Where(whereLambda).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 Update(T entity)
|
||||
{
|
||||
//所有字段均修改
|
||||
Db.Entry<T>(entity).State = EntityState.Modified;
|
||||
//return Db.SaveChanges() > 0;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
31
CC.Yi.DAL/CC.Yi.DAL.csproj
Normal file
31
CC.Yi.DAL/CC.Yi.DAL.csproj
Normal 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>
|
||||
50
CC.Yi.DAL/DbContentFactory.cs
Normal file
50
CC.Yi.DAL/DbContentFactory.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
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;
|
||||
|
||||
public DbContentFactory(DataContext webContext)
|
||||
{
|
||||
Webcontext = webContext;
|
||||
}
|
||||
|
||||
//public static void Initialize(DataContext webContext)
|
||||
//{
|
||||
// Webcontext = webContext;
|
||||
//}
|
||||
public static DataContext GetCurrentDbContent()
|
||||
{
|
||||
////return new DataModelContainer();
|
||||
////一次请求共用一个上下文实例
|
||||
////每一次http请求都会开启一个新的线程,保证在一个线程中,DbContext是唯一的
|
||||
////CallContext:是线程内部唯一的数据槽(一块内存空间/容器),相当于一个键值对数据容器,通过key获取value了,需要引入System.Runtime.Remoting.Messaging;命名空间
|
||||
//DbContext db = CallContext.GetData("DbContext") as DbContext;
|
||||
////从 CallContext 中检索具有指定key“DbContext”的对象,并强转为DbContext
|
||||
//if (db == null)//线程在数据槽里面没有此上下文
|
||||
//{
|
||||
// db = Webcontext;
|
||||
// CallContext.SetData("DbContext", db);//放到数据槽中去,DbContext是key,db是value
|
||||
//}
|
||||
//return db;
|
||||
return Webcontext;
|
||||
}
|
||||
|
||||
//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;
|
||||
//}
|
||||
}
|
||||
}
|
||||
15
CC.Yi.DAL/T4DAL.cs
Normal file
15
CC.Yi.DAL/T4DAL.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
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
|
||||
{
|
||||
public studentDal(DataContext _Db) : base(_Db)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
33
CC.Yi.DAL/T4DAL.tt
Normal file
33
CC.Yi.DAL/T4DAL.tt
Normal file
@@ -0,0 +1,33 @@
|
||||
<#@ 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
|
||||
{
|
||||
public <#=k #>Dal(DataContext _Db) : base(_Db)
|
||||
{
|
||||
}
|
||||
}
|
||||
<# } #>
|
||||
}
|
||||
Reference in New Issue
Block a user