This commit is contained in:
CortexCore
2023-10-02 23:24:56 +08:00
parent 8ef5c7ec0a
commit 947e52e748
183 changed files with 107857 additions and 9378 deletions

View File

@@ -0,0 +1,16 @@
{
"name": "BITKit.EntityFramework",
"rootNamespace": "",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"NET5_0_OR_GREATER"
],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,54 @@
#if NET5_0_OR_GREATER
using System;
using Cysharp.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace BITKit;
public interface IDatabaseContext<T> where T : class
{
void Add(T entity);
UniTask AddAsync(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 abstract class EntityFrameworkContext<T>:DbContext ,IDatabaseContext<T> where T : class
{
protected DbSet<T> context { get; private set; }
public void Add(T entity)
{
context.Add(entity);
SaveChanges();
}
public async UniTask AddAsync(T entity)
{
await context.AddAsync(entity);
await SaveChangesAsync();
}
public void Remove(T entity)
{
throw new NotImplementedException();
}
public T[] GetArray()
{
throw new NotImplementedException();
}
public bool TrySearch(Func<T, bool> searchFactory, out T result)
{
throw new NotImplementedException();
}
public bool TrySearchArray(Func<T, bool> searchFactory, out T[] result)
{
throw new NotImplementedException();
}
}
#endif

View File

@@ -0,0 +1,19 @@
#if NET5_0_OR_GREATER
using Microsoft.EntityFrameworkCore;
namespace BITKit;
public class MySQLContext<T>:DbContext where T:class
{
protected readonly string _connectSql;
protected DbSet<T> context { get; private set; }
public MySQLContext(string connectSql) : base()
{
_connectSql = connectSql;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySQL("_connectSql");
}
}
#endif

View File

@@ -0,0 +1,82 @@
#if NET5_0_OR_GREATER
using System;
using System.Linq;
using Cysharp.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace BITKit;
public class SqlLiteContext<T> : DbContext,IDatabaseContext<T> where T : class
{
public DbSet<T> context { get; private set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var sql = SQLiteContextHelper.GetConnectionString<T>();
optionsBuilder.UseSqlite(sql);
}
public virtual void Add(T entity)
{
context.Add(entity);
SaveChanges();
}
public UniTask AddAsync(T entity)
{
throw new NotImplementedException();
}
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;
}
}
public static class SQLiteContextHelper
{
private const string _database = "Database";
public static string GetConnectionString(string key)
{
GetConnectionSqlAndPath(key,out var connectionSql,out var path);
BIT4Log.Log($"已创建数据库链接:{path}");
return connectionSql;
}
public static string GetConnectionString<T>()
{
GetConnectionSqlAndPath(typeof(T).Name,out var connectionSql,out var path);
BIT4Log.Log<T>($"已创建数据库链接:{path}");
return connectionSql;
}
public static void GetConnectionSqlAndPath(string name,out string connectionSql,out string path)
{
PathHelper.GetFolderPath(_database);
path = PathHelper.GetPath(_database, $"{name}.db");
connectionSql = $"Data Source={path}";
}
}
#endif