254 lines
7.5 KiB
C#
254 lines
7.5 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;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Reflection;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
using Church.Net.DAL.EFCoreDBF.Interface;
|
|
|
|
namespace Church.Net.DAL.EFCoreDBF.Core
|
|
{
|
|
public class CombinedKeyCrudDALCBase<T> : ICombinedKeyCrudDAL<T> where T : class, ICombinedKeyEntity, new()
|
|
{
|
|
private readonly DatabaseOptions databaseOptions;
|
|
|
|
public CombinedKeyCrudDALCBase(DatabaseOptions databaseOptions)
|
|
{
|
|
this.databaseOptions = databaseOptions;
|
|
InitKeyProperty();
|
|
}
|
|
public IQueryable<T> GetDbSet()
|
|
{
|
|
//var result = (DbSet<T>)typeof(ChurchNetContext).GetMethod("Set").MakeGenericMethod(typeof(T)).Invoke(dbContext, null);
|
|
|
|
return databaseOptions.GetDbContext().Set<T>().AsNoTracking();
|
|
//return result;
|
|
}
|
|
|
|
public ChurchNetContext GetDbContext()
|
|
{
|
|
//var result = (DbSet<T>)typeof(ChurchNetContext).GetMethod("Set").MakeGenericMethod(typeof(T)).Invoke(dbContext, null);
|
|
|
|
return databaseOptions.GetDbContext();//.Set<T>().AsNoTracking();
|
|
//return result;
|
|
}
|
|
public virtual T First(Func<T, bool> filter = null)
|
|
{
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
return dbContext.Set<T>().Where(filter ?? (s => true)).FirstOrDefault();
|
|
}
|
|
}
|
|
|
|
public virtual T GetById(IEnumerable<string> Ids)
|
|
{
|
|
return GetQuery(Ids).FirstOrDefault();
|
|
}
|
|
|
|
//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().AsNoTracking().Where(filter ?? (s => true)).ToList();
|
|
}
|
|
public virtual IQueryable<T> GetQuery(Func<T, bool> filter = null)
|
|
{
|
|
//var dbObjs = GetDbSet().ToArray();
|
|
|
|
//IEnumerable<T> list = GetDbSet().Where(filter ?? (s => true)).ToList();
|
|
|
|
return GetDbSet().AsNoTracking().Where(filter ?? (s => true)).AsQueryable();
|
|
}
|
|
|
|
public virtual int Create(T entity)
|
|
{
|
|
CheckCombinedKeyIsEmpty(entity);
|
|
ConvertUTCTime(entity);
|
|
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
dbContext.Add(entity);
|
|
|
|
return dbContext.SaveChanges();
|
|
}
|
|
}
|
|
public virtual Task<int> CreateAsync(T entity)
|
|
{
|
|
CheckCombinedKeyIsEmpty(entity);
|
|
ConvertUTCTime(entity);
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
dbContext.Add(entity);
|
|
|
|
//CreateDone(entity, newDbObj);
|
|
return dbContext.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
|
|
public int CreateOrUpdate(T entity)
|
|
{
|
|
int result = 0;
|
|
ConvertUTCTime(entity);
|
|
if (CheckExist(entity))
|
|
{
|
|
result = Update(entity);
|
|
}
|
|
else
|
|
{
|
|
result = Create(entity);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public virtual int Update(T entity)
|
|
{
|
|
|
|
//if (!this.CheckExist(entity))
|
|
//{
|
|
// throw new ArgumentNullException("the Id is not exist.");
|
|
//}
|
|
ConvertUTCTime(entity);
|
|
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
dbContext.Update(entity);
|
|
|
|
return dbContext.SaveChanges();
|
|
}
|
|
|
|
|
|
}
|
|
public virtual void CreateDone(T entity)
|
|
{
|
|
|
|
}
|
|
public virtual void UpdateDone(T entity)
|
|
{
|
|
|
|
}
|
|
|
|
public virtual int UpdateRange(IEnumerable<T> entities)
|
|
{
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
dbContext.UpdateRange(entities);
|
|
|
|
return dbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public virtual int Delete(T obj)
|
|
{
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
dbContext.Remove(obj);
|
|
return dbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public virtual int Delete(IEnumerable<string> combinedKeyIds)
|
|
{
|
|
var obj = GetById(combinedKeyIds);
|
|
return obj == null ? Delete(obj) : 0;
|
|
}
|
|
|
|
public int Delete(Func<T, bool> filter)
|
|
{
|
|
var list = GetDbSet().Where(filter).ToList();
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
foreach (var item in list)
|
|
{
|
|
dbContext.Remove(item);
|
|
}
|
|
return dbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public virtual bool CheckExist(T obj)
|
|
{
|
|
//var query = GetDbSet().AsQueryable();
|
|
var query = GetAll();
|
|
List<PropertyInfo> props = new List<PropertyInfo>();
|
|
for (int i = 0; i < keyProps.Count; i++)
|
|
{
|
|
var prop = keyProps[i];
|
|
props.Add(prop);
|
|
var value = prop.GetValue(obj)?.ToString();
|
|
query = query.Where(e => prop.GetValue(e)?.ToString() == value).ToList();
|
|
}
|
|
|
|
return query.Any();
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private List<PropertyInfo> keyProps;
|
|
|
|
private void InitKeyProperty()
|
|
{
|
|
keyProps = typeof(T).GetProperties().Where(
|
|
prop => Attribute.IsDefined(prop, typeof(KeyAttribute))).OrderBy(prop =>
|
|
prop.GetCustomAttribute<ColumnAttribute>().Order
|
|
)
|
|
.ToList();
|
|
|
|
}
|
|
|
|
private IQueryable<T> GetQuery(IEnumerable<string> Ids)
|
|
{
|
|
using (var dbContext = GetDbContext())
|
|
{
|
|
var query = dbContext.Set<T>().AsQueryable();
|
|
|
|
for (int i = 0; i < keyProps.Count; i++)
|
|
{
|
|
query = query.Where(e => keyProps[i].GetValue(e).ToString() == Ids.ElementAt(i));
|
|
}
|
|
return query;
|
|
}
|
|
}
|
|
private void CheckCombinedKeyIsEmpty(T entity)
|
|
{
|
|
for (int i = 0; i < keyProps.Count; i++)
|
|
{
|
|
if (string.IsNullOrEmpty(keyProps[i].GetValue(entity)?.ToString()))
|
|
{
|
|
throw new Exception($"Combined Key [{keyProps[i].Name}] not defined!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
} |