step1:启动api项目  

step2:使用postman工具,填上接口地址http://localhost:7788/api/automapper/AutoMapperSuper

step3:表格数量过大,只是需要映射少部分表时,可以手动生成单表DTO,参数:{"tableName":"sys_option"}

// AutoMapperController.cs

namespace xxxxxxxxx
{
[Route("api/[controller]/")]
public class AutoMapperController : BaseController
{
IAutoMapperRepo _autoMapperManager;
public AutoMapperController(IAutoMapperRepo autoMapperManager)
{
_autoMapperManager = autoMapperManager;
} [HttpPost("AutoMapperSuper")]
public ApiResponse AutoMapperSuper(AutoMapperHotelReq req)
{
return _autoMapperManager.AutoMapperSuper(req);
}
}
}
// AutoMapperSuper.cs

namespace DLHIS.X8.Repository
{
public class AutoMapperRepo : BaseRepo, IAutoMapperRepo
{
List<string> listDescription = new List<string>(); public ApiResponse AutoMapperSuper(AutoMapperHotelReq req)
{
try
{
IDbRes superres = HotelPool.GetSuperDbRes();
if (superres.Code != )
{
return Failed(superres.Message);
}
using (superres.conn)
{
if (req.TableName != "")
{
var listtb = superres.conn.DbMaintenance.GetTableInfoList();
string[] s = req.TableName.Split(new char[] { '_' });
var count = s.Length;
string ModelName = "";
string s1 = "";
string s2 = "";
for (var i = ; i < s.Length; i++)
{
s1 = (s[i].Substring(, )).ToUpper();
s2 = (s[i].Substring());
if (s2 == null)
{
s2 = "";
}
ModelName = ModelName + s1 + s2;
}
ModelName = ModelName + "Model";
superres.conn.MappingTables.Add(ModelName, req.TableName);
var coltb = superres.conn.DbMaintenance.GetColumnInfosByTableName(req.TableName);
foreach (DbColumnInfo c in coltb)
{
string[] s4 = c.DbColumnName.Split(new char[] { '_' });
var count4 = s4.Length;
string colname = "";
string s41 = "";
string s42 = "";
for (var j = ; j < s4.Length; j++)
{
if (s4[j].Length <= )
{
s41 = s4[j].ToUpper();
s42 = "";
}
else
{
s41 = (s4[j].Substring(, )).ToUpper();
s42 = (s4[j].Substring());
}
if (s42 == null)
{
s42 = "";
}
colname = colname + s41 + s42;
}
superres.conn.MappingColumns.Add(colname, c.DbColumnName, ModelName);
}
superres.conn.DbFirst.IsCreateAttribute().Where(req.TableName).CreateClassFile("D:\\AutoMapper\\DTO", "DLHIS.X8.Entities.DTO");
}
else
{
var listtb = superres.conn.DbMaintenance.GetTableInfoList();
foreach (DbTableInfo t in listtb)
{
string[] s = t.Name.Split(new char[] { '_' });
var count = s.Length;
string ModelName = "";
string s1 = "";
string s2 = "";
for (var i = ; i < s.Length; i++)
{
s1 = (s[i].Substring(, )).ToUpper();
s2 = (s[i].Substring());
if (s2 == null)
{
s2 = "";
}
ModelName = ModelName + s1 + s2;
}
ModelName = ModelName + "Model";
superres.conn.MappingTables.Add(ModelName, t.Name);
var coltb = superres.conn.DbMaintenance.GetColumnInfosByTableName(t.Name);
foreach (DbColumnInfo c in coltb)
{
string[] s4 = c.DbColumnName.Split(new char[] { '_' });
var count4 = s4.Length;
string colname = "";
string s41 = "";
string s42 = "";
for (var j = ; j < s4.Length; j++)
{
if (s4[j].Length <= )
{
s41 = s4[j].ToUpper();
s42 = "";
}
else
{
s41 = (s4[j].Substring(, )).ToUpper();
s42 = (s4[j].Substring());
}
if (s42 == null)
{
s42 = "";
}
colname = colname + s41 + s42;
}
superres.conn.MappingColumns.Add(colname, c.DbColumnName, ModelName);
}
superres.conn.DbFirst.IsCreateAttribute().Where(t.Name).CreateClassFile("D:\\AutoMapper\\DTO", "DLHIS.X8.Entities.DTO");
}
}
return Success();
}
}
catch (Exception e)
{
return Failed("自动生成数据库实体类时发生异常,错误描述:" + e.Message);
}
}
}
}
// IAutoMapperRepo.cs

namespace xxxxxxxxxxxxxxx
{
public interface IAutoMapperRepo
{
ApiResponse AutoMapperSuper(AutoMapperHotelReq req);
}
}

附:链接数据库操作

public static IDbRes GetSuperDbRes()
{
string ConStr = "";
IDbRes res = new IDbRes();
try
{
/*
MyDbContext dbconn = Global.LoveHotelDbContext;
if (dbconn == null)
{
Global.LoveHotelDbContext = new MyDbContext();
dbconn = new MyDbContext();
}
var item = dbconn.HotelDbConns.FirstOrDefault(x => x.HotelId == "SuperDb");
if (item != null)
{
res.Code = 200;
res.Message = "获取数据库连接池成功!";
res.conn = item.DbConn;
item.RefreshTime = DateTime.Now;
return res;
}
else
{
*/
string jsonfile = "appsettings.json";
using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject o = (JObject)JToken.ReadFrom(reader);
ConStr = Convert.ToString(o["ConnectionString"]);
}
}
SqlSugarClient db = new SqlSugarClient(
new ConnectionConfig()
{
ConnectionString = ConStr,
DbType = DbType.SqlServer,//设置数据库类型
IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
});
/*
HotelDbConn tmp = new HotelDbConn();
tmp.HotelId = "SuperDb";
tmp.DbConn = db;
tmp.RefreshTime = DateTime.Now;
dbconn.HotelDbConns.Add(tmp);
*/
res.Code = ;
res.conn = db;
return res;
//}
}
catch (Exception e)
{
res.Code = ;
res.Message = "连接数据库时发生异常" + e.Message + "###:" + ConStr;
return res;
}
}
05-27 21:05