表实体上面添加特性获取到连接字符串

public static string ConnectionString(Type type)
{
try
{
string tableName = type.Name;
string configName = null;
object[] arrAttributes = type.GetCustomAttributes(typeof(Attribute), true);
if (arrAttributes != null)
{
TableNameAttribute table = arrAttributes.FirstOrDefault(x => x.GetType().Name.Equals(typeof(TableNameAttribute).Name)) as TableNameAttribute;
if (table != null)
{
tableName = table.TableName;
configName = table.ConfigName;
}
}

if (string.IsNullOrWhiteSpace(configName))
{
configName = GetConfigName(tableName);
}

if (string.IsNullOrWhiteSpace(configName))
{
return ConnectionString();
}

return ConnectionString(configName);
}
catch
{
return ConnectionString();
}
}

--------------------------------------------------------表实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TRS.Export.FrameEntity.Attributes;

namespace TRS.Export.Entity
{
/// <summary>
/// TWX_TransportOrder
/// </summary>
[Serializable]
[TableName("TWX_TransportOrder")]
public partial class TWX_TransportOrder
{
private long? _transportorderid;//转运单主键
private string _transportordercode;//转运单号 出库号
private long? _tradeorderid;//淘宝交易号
private string _taobaologisticsid;//物流订单的订单号
private int? _warehouseid;//虚拟仓库地址
private DateTime? _warehouseouttime;//出库时间
private int? _warehouseoutuser;//出库人
private string _interdeliverycode;//国际物流单号

}

-----------------------------------------------------------TableNameAttribute

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TRS.Export.FrameEntity.Attributes
{
public class TableNameAttribute : Attribute
{
public string TableName { get; set; }

public string ConfigName { get; set; }

public TableNameAttribute(string tableName, string configName = null)
{
TableName = tableName;
ConfigName = configName;
}
}
}

原文地址:https://www.cnblogs.com/chengjun/p/8398872.html