v1.0.1
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
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;
|
||||
|
||||
@@ -9,10 +11,9 @@ namespace CC.Yi.DAL
|
||||
{
|
||||
public class BaseDal<T> : IBaseDal<T> where T : class, new()
|
||||
{
|
||||
public DataContext Db;
|
||||
public BaseDal(DataContext _Db)
|
||||
public DbContext Db
|
||||
{
|
||||
Db = _Db;
|
||||
get { return DbContentFactory.GetCurrentDbContent(); }
|
||||
}
|
||||
public IQueryable<T> GetEntities(Expression<Func<T, bool>> whereLambda)
|
||||
{
|
||||
@@ -65,6 +66,11 @@ namespace CC.Yi.DAL
|
||||
//Db.SaveChanges();
|
||||
return entity;
|
||||
}
|
||||
public bool AddRange(IEnumerable<T> entities)
|
||||
{
|
||||
Db.Set<T>().AddRange(entities);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Update(T entity)
|
||||
{
|
||||
@@ -73,7 +79,16 @@ namespace CC.Yi.DAL
|
||||
//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)
|
||||
{
|
||||
@@ -87,6 +102,12 @@ namespace CC.Yi.DAL
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,43 +8,36 @@ namespace CC.Yi.DAL
|
||||
{
|
||||
public class DbContentFactory
|
||||
{
|
||||
private static DataContext Webcontext;
|
||||
|
||||
public DbContentFactory(DataContext webContext)
|
||||
private static DataContext Webcontext;
|
||||
public static void Initialize(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;
|
||||
//return new DataModelContainer();
|
||||
//一次请求共用一个上下文实例
|
||||
//每一次http请求都会开启一个新的线程,保证在一个线程中,DbContext是唯一的
|
||||
//CallContext:是线程内部唯一的数据槽(一块内存空间/容器),相当于一个键值对数据容器,通过key获取value了,需要引入System.Runtime.Remoting.Messaging;命名空间
|
||||
DataContext db = CallContext.GetData("DbContext") as DataContext;
|
||||
//从 CallContext 中检索具有指定key“DbContext”的对象,并强转为DbContext
|
||||
if (db == null)//线程在数据槽里面没有此上下文
|
||||
{
|
||||
db = Webcontext;
|
||||
CallContext.SetData("DbContext", db);//放到数据槽中去,DbContext是key,db是value
|
||||
}
|
||||
return db;
|
||||
}
|
||||
|
||||
//private static class CallContext
|
||||
//{
|
||||
// static ConcurrentDictionary<string, AsyncLocal<object>> state = new ConcurrentDictionary<string, AsyncLocal<object>>();
|
||||
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 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;
|
||||
//}
|
||||
public static object GetData(string name) =>
|
||||
state.TryGetValue(name, out AsyncLocal<object> data) ? data.Value : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,5 @@ namespace CC.Yi.DAL
|
||||
{
|
||||
public partial class studentDal : BaseDal<student>, IstudentDal
|
||||
{
|
||||
public studentDal(DataContext _Db) : base(_Db)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,9 +25,6 @@ namespace CC.Yi.DAL
|
||||
#>
|
||||
public partial class <#=k #>Dal : BaseDal<<#=k #>>, I<#=k #>Dal
|
||||
{
|
||||
public <#=k #>Dal(DataContext _Db) : base(_Db)
|
||||
{
|
||||
}
|
||||
}
|
||||
<# } #>
|
||||
}
|
||||
Reference in New Issue
Block a user