205 lines
5.7 KiB
C#
205 lines
5.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System.Collections.Generic;
|
|
using System.Transactions;
|
|
using System;
|
|
using Church.Net.DAL.EF;
|
|
using System.Linq;
|
|
using Church.Net.Utility;
|
|
using System.Threading.Tasks;
|
|
using Church.Net.Entity.Interface;
|
|
|
|
namespace Church.Net.DAL.EFCoreDBF
|
|
{
|
|
public class CrudDALCBase<T> : ICrudDAL<T> where T : class, Church.Net.Entity.Interface.IEntity, new()
|
|
{
|
|
private readonly ChurchNetContext dbContext;
|
|
|
|
public CrudDALCBase(ChurchNetContext dbContext)
|
|
{
|
|
this.dbContext = dbContext;
|
|
}
|
|
public DbSet<T> GetDbSet()
|
|
{
|
|
//var result = (DbSet<T>)typeof(ChurchNetContext).GetMethod("Set").MakeGenericMethod(typeof(T)).Invoke(dbContext, null);
|
|
|
|
return (DbSet<T>)dbContext.Set<T>();
|
|
//return result;
|
|
}
|
|
|
|
public virtual T First(Func<T, bool> filter = null)
|
|
{
|
|
return GetDbSet().Where(filter ?? (s => true)).FirstOrDefault();
|
|
}
|
|
|
|
public virtual T GetById(string Id)
|
|
{
|
|
return GetDbSet().FirstOrDefault(e => e.Id == Id);
|
|
}
|
|
|
|
//public virtual T GetByRefndx(string refndx)
|
|
//{
|
|
// return MapDBModelToEntity(GetFirstDBModelByRefndx<ST>(refndx, this.actionScreen));
|
|
//}
|
|
|
|
public virtual IEnumerable<T> GetAll(Func<T, bool> filter = null)
|
|
{
|
|
//var dbObjs = GetDbSet().ToArray();
|
|
|
|
//IEnumerable<T> list = GetDbSet().Where(filter ?? (s => true)).ToList();
|
|
|
|
return GetDbSet().Where(filter ?? (s => true)).ToList();
|
|
}
|
|
|
|
public virtual IEnumerable<T> GetAllById(IEnumerable<string> Ids)
|
|
{
|
|
var RowIds = Ids.Select(i => i.ToString());
|
|
|
|
//return GetDbSet().Where(e => RowIds.Any(id => id == e.Id));
|
|
|
|
//var list = new List<T>();
|
|
var dbObjs = GetDbSet().Where(e => RowIds.Any(id => id == e.Id)).ToArray();
|
|
|
|
return dbObjs.ToList();
|
|
}
|
|
public virtual int Create(T entity)
|
|
{
|
|
if (string.IsNullOrEmpty(entity.Id))
|
|
{
|
|
entity.Id = StringHelper.Get33BaseGuid();
|
|
}
|
|
|
|
this.ConvertUTCTime(entity);
|
|
dbContext.Add(entity);
|
|
|
|
return dbContext.SaveChanges();
|
|
}
|
|
public virtual Task<int> CreateAsync(T entity)
|
|
{
|
|
int result = 0;
|
|
this.ConvertUTCTime(entity);
|
|
if (string.IsNullOrEmpty(entity.Id))
|
|
{
|
|
entity.Id = StringHelper.Get33BaseGuid();
|
|
}
|
|
T newDbObj = new T();
|
|
if (newDbObj != null)
|
|
{
|
|
//AfterCreatMapping(entity, newDbObj);
|
|
//newDbObj.RowId = entity.Id.ToString();
|
|
//result = Add(newDbObj) ? 1 : 0;
|
|
}
|
|
dbContext.Add(entity);
|
|
|
|
//CreateDone(entity, newDbObj);
|
|
return dbContext.SaveChangesAsync();
|
|
}
|
|
public virtual string CreateReturnId(T entity)
|
|
{
|
|
this.ConvertUTCTime(entity);
|
|
if (Create(entity) > 0)
|
|
{
|
|
return entity.Id;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
};
|
|
}
|
|
|
|
public int CreateOrUpdate(T entity, out string id)
|
|
{
|
|
id = null;
|
|
this.ConvertUTCTime(entity);
|
|
if (CheckExist(entity))
|
|
{
|
|
dbContext.Update(entity);
|
|
}
|
|
else
|
|
{
|
|
if (string.IsNullOrEmpty(entity.Id))
|
|
{
|
|
entity.Id = StringHelper.Get33BaseGuid();
|
|
}
|
|
id = entity.Id;
|
|
dbContext.Add(entity);
|
|
}
|
|
return dbContext.SaveChanges();
|
|
}
|
|
|
|
public virtual int Update(T entity)
|
|
{
|
|
|
|
var dbObj = GetDbSet().FirstOrDefault(e => e.Id == entity.Id);
|
|
if (dbObj == null)
|
|
{
|
|
throw new ArgumentNullException("the Id is not exist.");
|
|
}
|
|
this.ConvertUTCTime(entity);
|
|
|
|
dbContext.Update(dbObj);
|
|
|
|
return dbContext.SaveChanges();
|
|
|
|
|
|
}
|
|
public virtual void CreateDone(T entity)
|
|
{
|
|
|
|
}
|
|
public virtual void UpdateDone(T entity)
|
|
{
|
|
|
|
}
|
|
|
|
public virtual int UpdateRange(IEnumerable<T> entities)
|
|
{
|
|
foreach (var entity in entities)
|
|
{
|
|
this.ConvertUTCTime(entity);
|
|
}
|
|
dbContext.UpdateRange(entities);
|
|
|
|
return dbContext.SaveChanges();
|
|
}
|
|
|
|
public virtual int Delete(T obj)
|
|
{
|
|
dbContext.Remove(GetDbSet().FirstOrDefault(e => e.Id == obj.Id.ToString()));
|
|
return dbContext.SaveChanges();
|
|
}
|
|
|
|
public int Delete(Func<T, bool> filter)
|
|
{
|
|
var list = GetDbSet().Where(filter).ToList();
|
|
if (list.Count > 0)
|
|
{
|
|
dbContext.RemoveRange(list);
|
|
return dbContext.SaveChanges();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public virtual bool CheckExist(T obj)
|
|
{
|
|
return GetDbSet().Any(e => e.Id == obj.Id);
|
|
}
|
|
|
|
private void ConvertUTCTime(T entity)
|
|
{
|
|
|
|
var props = typeof(T).GetProperties();
|
|
foreach (var prop in props)
|
|
{
|
|
if (prop.PropertyType == typeof(DateTime))
|
|
{
|
|
//do stuff like prop.SetValue(t, DateTime.Now, null);
|
|
DateTime utcTime = ((DateTime)prop.GetValue(entity)).ToUniversalTime();
|
|
prop.SetValue(entity, utcTime, null);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
} |