This commit is contained in:
CortexCore
2023-10-06 23:43:19 +08:00
parent ebf9c1f526
commit 2c4710bc5d
186 changed files with 111802 additions and 764 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,7 @@
fileFormatVersion: 2
guid: a3f593891073dfc4c860f7df41ef8e2c
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

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,11 @@
fileFormatVersion: 2
guid: 355b442bac74fd443b42f7f601c5a824
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

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,11 @@
fileFormatVersion: 2
guid: 88296b712b21d1549acf51a95b74e126
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

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

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 051098367a2e84d4c9ade3bc7ef6eb1e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: