You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.0 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Txgy.EWS.Server.ICommon;
using Txgy.EWS.Server.IService;
namespace Txgy.EWS.Server.Service
{
public class BaseService : IBaseService
{
protected DbContext Context { get; private set; }
public BaseService(IDbConnectionFactory contextFactory)
{
Context = contextFactory.CreateDbContext();
Context.Database.EnsureCreated();// 对于数据库、表结构更新无效
}
public void Commit()
{
this.Context.SaveChanges();
}
public void Delete<T>(int Id) where T : class
{
T t = this.Find<T>(Id);//也可以附加
if (t == null) throw new Exception("t is null");
this.Context.Set<T>().Remove(t);
this.Commit();
}
public void Delete<T>(T t) where T : class
{
if (t == null) throw new Exception("t is null");
this.Context.Set<T>().Attach(t);
this.Context.Set<T>().Remove(t);
this.Commit();
}
public void Delete<T>(IEnumerable<T> tList) where T : class
{
foreach (var t in tList)
{
this.Context.Set<T>().Attach(t);
}
this.Context.Set<T>().RemoveRange(tList);
this.Commit();
}
public T Find<T>(int id) where T : class
{
return this.Context.Set<T>().Find(id);
}
public T Insert<T>(T t) where T : class
{
this.Context.Set<T>().Add(t);
this.Commit();
return t;
}
public IEnumerable<T> Insert<T>(IEnumerable<T> tList) where T : class
{
this.Context.Set<T>().AddRange(tList);
this.Commit();//写在这里 就不需要单独commit 不写就需要
return tList;
}
public IQueryable<T> Query<T>(Expression<Func<T, bool>> funcWhere) where T : class
{
return this.Context.Set<T>().Where<T>(funcWhere);
}
public void Update<T>(T t) where T : class
{
if (t == null) throw new Exception("t is null");
this.Context.Set<T>().Attach(t);//将数据附加到上下文支持实体修改和新实体重置为UnChanged
this.Context.Entry<T>(t).State = EntityState.Modified;
this.Commit();
}
public void Update<T>(IEnumerable<T> tList) where T : class
{
foreach (var t in tList)
{
this.Context.Set<T>().Attach(t);
this.Context.Entry<T>(t).State = EntityState.Modified;
}
this.Commit();
}
public virtual void Dispose()
{
if (this.Context != null)
{
this.Context.Dispose();
}
}
}
}