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

184 lines
6.3 KiB

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 MySqlConnector;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using VOL.Core.Configuration;
using VOL.Core.Const;
using VOL.Core.Dapper;
using VOL.Core.EFDbContext;
using VOL.Core.Enums;
using VOL.Core.Extensions;
namespace VOL.Core.DBManager
{
public partial class DBServerProvider
{
private static Dictionary<string, string> ConnectionPool = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private static readonly string DefaultConnName = "defalut";
static DBServerProvider()
{
SetConnection(DefaultConnName, AppSetting.DbConnectionString);
}
public static void SetConnection(string key, string val)
{
if (ConnectionPool.ContainsKey(key))
{
ConnectionPool[key] = val;
return;
}
ConnectionPool.Add(key, val);
}
/// <summary>
/// 设置默认数据库连接
/// </summary>
/// <param name="val"></param>
public static void SetDefaultConnection(string val)
{
SetConnection(DefaultConnName, val);
}
public static string GetConnectionString(string key)
{
key = key ?? DefaultConnName;
if (ConnectionPool.ContainsKey(key))
{
return ConnectionPool[key];
}
return key;
}
/// <summary>
/// 获取默认数据库连接
/// </summary>
/// <returns></returns>
public static string GetConnectionString()
{
return GetConnectionString(DefaultConnName);
}
public static IDbConnection GetDbConnection(string connString = null)
{
if (connString==null)
{
connString = ConnectionPool[DefaultConnName];
}
if (DBType.Name == DbCurrentType.MySql.ToString())
{
return new MySqlConnection(connString);
}
if (DBType.Name == DbCurrentType.PgSql.ToString())
{
return new NpgsqlConnection(connString);
}
return new SqlConnection(connString);
}
/// <summary>
/// 扩展dapper 获取MSSQL数据库DbConnection默认系统获取配置文件的DBType数据库类型
/// </summary>
/// <param name="connString">如果connString为null 执行重载GetDbConnection(string connString = null)</param>
/// <param name="dapperType">指定连接数据库的类型MySql/MsSql/PgSql</param>
/// <returns></returns>
public static IDbConnection GetDbConnection(string connString = null, DbCurrentType dbCurrentType=DbCurrentType.Default)
{
//默认获取DbConnection
if (connString.IsNullOrEmpty()|| DbCurrentType.Default== dbCurrentType)
{
return GetDbConnection(connString);
}
if (dbCurrentType==DbCurrentType.MySql)
{
return new MySqlConnection(connString);
}
if (dbCurrentType == DbCurrentType.PgSql)
{
return new NpgsqlConnection(connString);
}
return new SqlConnection(connString);
}
public static VOLContext DbContext
{
get { return GetEFDbContext(); }
}
public static VOLContext GetEFDbContext()
{
return GetEFDbContext(null);
}
public static VOLContext GetEFDbContext(string dbName)
{
VOLContext context = Utilities.HttpContext.Current.RequestServices.GetService(typeof(VOLContext)) as VOLContext;
if (dbName != null)
{
if (!ConnectionPool.ContainsKey(dbName))
{
throw new Exception("数据库连接名称错误");
}
context.Database.GetDbConnection().ConnectionString = ConnectionPool[dbName];
}
return context;
}
public static void SetDbContextConnection(VOLContext beefContext, string dbName)
{
if (!ConnectionPool.ContainsKey(dbName))
{
throw new Exception("数据库连接名称错误");
}
beefContext.Database.GetDbConnection().ConnectionString = ConnectionPool[dbName];
}
/// <summary>
/// 获取实体的数据库连接
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="defaultDbContext"></param>
/// <returns></returns>
public static void GetDbContextConnection<TEntity>(VOLContext defaultDbContext)
{
//string connstr= defaultDbContext.Database.GetDbConnection().ConnectionString;
// if (connstr != ConnectionPool[DefaultConnName])
// {
// defaultDbContext.Database.GetDbConnection().ConnectionString = ConnectionPool[DefaultConnName];
// };
}
public static ISqlDapper SqlDapper
{
get
{
return new SqlDapper(DefaultConnName);
}
}
public static ISqlDapper GetSqlDapper(string dbName = null)
{
return new SqlDapper(dbName ?? DefaultConnName);
}
/// <summary>
///
/// </summary>
/// <param name="dbCurrentType">指定数据库类型MySql/MsSql/PgSql</param>
/// <param name="dbName">指定数据连串名称</param>
/// <returns></returns>
public static ISqlDapper GetSqlDapper(DbCurrentType dbCurrentType, string dbName = null)
{
if (dbName.IsNullOrEmpty())
{
return new SqlDapper(dbName ?? DefaultConnName);
}
return new SqlDapper(dbName, dbCurrentType);
}
public static ISqlDapper GetSqlDapper<TEntity>()
{
//获取实体真实的数据库连接池对象名,如果不存在则用默认数据连接池名
string dbName = typeof(TEntity).GetTypeCustomValue<DBConnectionAttribute>(x => x.DBName) ?? DefaultConnName;
return GetSqlDapper(dbName);
}
}
}