IDIS
This commit is contained in:
@@ -15,14 +15,11 @@ public interface IDatabaseContext<T> where T : class
|
||||
|
||||
public class SqlLiteContext<T> : DbContext,IDatabaseContext<T> where T : class
|
||||
{
|
||||
private const string _database = "Database";
|
||||
|
||||
public DbSet<T> context { get; private set; }
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
PathHelper.GetFolderPath(_database);
|
||||
var path = PathHelper.GetPath(_database, $"{typeof(T).Name}.db");
|
||||
var sql = $"Data Source={path}";
|
||||
BIT4Log.Log<T>($"已创建数据库链接:{path}");
|
||||
var sql = SQLiteContextHelper.GetConnectionString<T>();
|
||||
optionsBuilder.UseSqlite(sql);
|
||||
}
|
||||
public virtual void Add(T entity)
|
||||
@@ -55,4 +52,30 @@ public class SqlLiteContext<T> : DbContext,IDatabaseContext<T> where T : class
|
||||
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}";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user