更新了生产过程
This commit is contained in:
@@ -1,8 +1,19 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BITKit;
|
||||
|
||||
public class SqlLiteContext<T> : DbContext where T : class
|
||||
public interface IDatabaseContext<T> where T : class
|
||||
{
|
||||
void Add(T entity);
|
||||
void Remove(T entity);
|
||||
T[] GetArray();
|
||||
bool TrySearch(Func<T, bool> searchFactory, out T result);
|
||||
bool TrySearchArray(Func<T, bool> searchFactory, out T[] result);
|
||||
}
|
||||
|
||||
public class SqlLiteContext<T> : DbContext,IDatabaseContext<T> where T : class
|
||||
{
|
||||
private const string _database = "Database";
|
||||
public DbSet<T> context { get; private set; }
|
||||
@@ -14,4 +25,34 @@ public class SqlLiteContext<T> : DbContext where T : class
|
||||
BIT4Log.Log<T>($"已创建数据库链接:{path}");
|
||||
optionsBuilder.UseSqlite(sql);
|
||||
}
|
||||
public virtual void Add(T entity)
|
||||
{
|
||||
context.Add(entity);
|
||||
SaveChanges();
|
||||
}
|
||||
public virtual void Remove(T entity)
|
||||
{
|
||||
context.Remove(entity);
|
||||
SaveChanges();
|
||||
}
|
||||
public virtual T[] GetArray()
|
||||
{
|
||||
return context.ToArray();
|
||||
}
|
||||
public virtual bool TrySearch(Func<T,bool> searchFactory,out T result)
|
||||
{
|
||||
result = context.FirstOrDefault(searchFactory);
|
||||
return result != null;
|
||||
}
|
||||
/// <summary>
|
||||
/// 搜索数组
|
||||
/// </summary>
|
||||
/// <param name="searchFactory"></param>
|
||||
/// <param name="result"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool TrySearchArray(Func<T, bool> searchFactory, out T[] result)
|
||||
{
|
||||
result = context.Where(searchFactory).ToArray();
|
||||
return result.Length > 0;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user