/// <summary> /// 根据数据表生成Model层、Controller层、IRepository层和Repository层代码 /// </summary> /// <param name="ifExsitedCovered">是否覆盖已存在的同名文件</param> public static void GenerateAllCodesFromDatabase(bool ifExsitedCovered = false) { var dbContext = AspectCoreContainer.Resolve <IDbContextCore>(); if (dbContext == null) { throw new Exception("未能获取到数据库上下文,请先注册数据库上下文。"); } var tables = dbContext.GetCurrentDatabaseTableList(); if (tables != null && tables.Any()) { foreach (var table in tables) { if (table.Columns.Any(c => c.IsPrimaryKey)) { var pkTypeName = table.Columns.First(m => m.IsPrimaryKey).CSharpType; GenerateEntity(table, ifExsitedCovered); GenerateIRepository(table.TableName.ToPascalCase(), pkTypeName, ifExsitedCovered); GenerateRepository(table.TableName.ToPascalCase(), pkTypeName, ifExsitedCovered); GenerateController(table.TableName.ToPascalCase(), pkTypeName, ifExsitedCovered); } } } }
/// <summary> /// 静态构造函数:从IoC容器读取配置参数,如果读取失败则会抛出ArgumentNullException异常 /// </summary> static CodeGenerator() { options = AspectCoreContainer.Resolve <IOptions <CodeGenerateOption> >(); if (options == null) { throw new ArgumentNullException(nameof(options)); } var path = AppDomain.CurrentDomain.BaseDirectory; var flag = path.IndexOf("/bin"); if (flag > 0) { Delimiter = "/";//如果可以取到值,修改分割符 } }