private void Btn_convert_Click(object sender, EventArgs e) { string allModifiedStoredProcdures = ""; string allModelBuilderQuery = ""; DeleteFolder("AsyadCapital.Entities", true); try { DB_Helper db_helper = new DB_Helper(); string DBName = ""; if (db_helper.CheckConnectionStringValidity(txt_ConnectionString.Text, db_helper.GetNamespaceProvider(cmb_DBProvider.Text)) == true) { //Get Db Name From Connection String DBName = (db_helper.GetConnectionStringBuilder(txt_ConnectionString.Text)?["database"] ?? db_helper.GetConnectionStringBuilder(txt_ConnectionString.Text)?["Initial Catalog"]) as string; if (DBName == null) { MessageBox.Show("Coonection String is not valid", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { MessageBox.Show("DB Connection Failed", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //MakeMVCScaffolding(); if (txt_Source.Text == "") { MessageBox.Show("Please Enter MVC Context File To Convert", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } SyntaxTree tree = CSharpSyntaxTree.ParseText(txt_Source.Text); CompilationUnitSyntax root = tree.GetCompilationUnitRoot(); //Get Context Class ClassDeclarationSyntax mvcContextclass = root.DescendantNodes().OfType <ClassDeclarationSyntax>() ?.Where(c => c?.BaseList.Types.FirstOrDefault().Type.ToString() == "DbContext")?.FirstOrDefault(); if (mvcContextclass == null) { MessageBox.Show("Please Add File contain MVC context class ", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //DO Core Scafoollding lbl_load.Text = "please wait untit convert Text File......."; MakeCoreScaffolding(); lbl_load.Text = "File is Converted"; //Get Scafoolding Core File string Core_ContextFilePath = Path.GetFullPath($@"AsyadCapital.Entities\DBEntities\{DBName}Context.cs"); StreamReader sr_ScafooldingFile = new StreamReader(Core_ContextFilePath); string Core_ContextFile = sr_ScafooldingFile.ReadToEnd(); SyntaxTree coreTree = CSharpSyntaxTree.ParseText(Core_ContextFile); CompilationUnitSyntax coreRoot = coreTree.GetCompilationUnitRoot(); //Get Core Context Class ClassDeclarationSyntax Core_contextclass = coreRoot.DescendantNodes() .OfType <ClassDeclarationSyntax>() ?.Where(c => c?.BaseList.Types.FirstOrDefault().Type.ToString() == "DbContext")?.FirstOrDefault(); //Get old OnModelCreating method var coreOldOnModelCreatingMethod = Core_contextclass?.DescendantNodes() .OfType <MethodDeclarationSyntax>()?.FirstOrDefault(m => m.Identifier.Text == "OnModelCreating"); if (Core_contextclass == null || coreOldOnModelCreatingMethod == null) { MessageBox.Show("Error with Scafooling From DB ", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Get Stored Procdures inside Context Class var mvcAllStoredProcedurs = mvcContextclass.DescendantNodes().OfType <MethodDeclarationSyntax>()?.Where( m => m.Body.ToFullString() .Contains("return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction")).ToList(); if (mvcAllStoredProcedurs.Count == 0) { MessageBox.Show("Any stored Procures not Exists ", null, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // loop on stored Procdures foreach (var sp in mvcAllStoredProcedurs) { var spAccessModifier = sp.Modifiers.ToFullString(); var spMethodName = sp.Identifier; var spMethodParamters = sp.ParameterList; string spDeclationStatmentList = ""; string spExecuteParamter = ""; var spReturnType = (sp.ReturnType as GenericNameSyntax)?.TypeArgumentList.Arguments[0] ?? sp.ReturnType; var s = spReturnType.ToFullString(); if (sp.ReturnType.GetFirstToken().Text == "ObjectResult") { if ((spReturnType).GetType() == typeof(IdentifierNameSyntax)) { //add ModelBuilderQuery allModelBuilderQuery += $"modelBuilder.Query<{spReturnType}>();\n"; } string firstsection = $"{spAccessModifier}async Task<List<{spReturnType}>>{spMethodName}{spMethodParamters}" + "\n{" + "\n// Initialization.\n" + $"List<{spReturnType}> {spMethodName}_List = new List<{spReturnType}>();\n" + "try\n" + "{" + "\n// Sql Parameters . \n"; var declationStatmentList = sp.Body.Statements.OfType <LocalDeclarationStatementSyntax>().ToList(); foreach (var param in declationStatmentList) { string VariableName = param.Declaration.Variables.First().Identifier.Text; spDeclationStatmentList += $" {cmb_DBProvider.Text} {VariableName} = new {cmb_DBProvider.Text}(" + $"\"@{FirstLetterToupper(VariableName).Substring(0, VariableName.Length - 9)}\"" + $", {VariableName.Substring(0, VariableName.Length - 9)} ?? (object)DBNull.Value);\n"; } string spExecuteStatment = ""; // if (!Sp_ReturnType.GetType().IsValueType) if ((spReturnType) is IdentifierNameSyntax || spReturnType.ToFullString().ToLower() == "string") { spExecuteStatment = "// Create Query . \n" + $"var query = Query<{spReturnType}>().FromSql(\"EXEC [dbo].[{spMethodName}] "; } else { spExecuteStatment = "// Create Query . \n" + $"var query = (IQueryable< {spReturnType} >) Query<Object>().FromSql(\"EXEC[dbo].[{spMethodName}] "; } for (int k = 0; k < declationStatmentList.Count; k++) { spExecuteParamter += $"@{spMethodParamters.Parameters[k].Identifier}={{{k}}}"; if (k < declationStatmentList.Count - 1) { spExecuteParamter += ","; } } spExecuteParamter += $";\""; foreach (var declationStatment in declationStatmentList) { spExecuteParamter += $",{declationStatment.Declaration.Variables.First().Identifier.Text}"; } spExecuteParamter += $");\n{spMethodName}_List = await query.ToListAsync();\n" + "}\n catch (Exception ex)\n{\nthrow ex;\n}\n return" + $" {spMethodName}_List;\n" + "}\n"; allModifiedStoredProcdures += firstsection + spDeclationStatmentList + spExecuteStatment + spExecuteParamter; } else { string firstsection = $"{spAccessModifier}async Task<{spReturnType}>{spMethodName}{spMethodParamters}" + "\n{" + "\n// Initialization.\n" + $"{spReturnType} result = default({spReturnType}) ;\n" + "try\n" + "{" + "\n// Sql Parameters . \n"; var declationStatmentList = sp.Body.Statements.OfType <LocalDeclarationStatementSyntax>().ToList(); foreach (var param in declationStatmentList) { string VariableName = param.Declaration.Variables.First().Identifier.Text; spDeclationStatmentList += $" {cmb_DBProvider.Text} {VariableName} = new {cmb_DBProvider.Text}(" + $"\"@{FirstLetterToupper(VariableName).Substring(0, VariableName.Length - 9)}\"" + $", {VariableName.Substring(0, VariableName.Length - 9)} ?? (object)DBNull.Value);\n"; } string Sp_ExecuteStatment = "// Create Query . \n" + $"var query = Query<object>().FromSql(\"EXEC [dbo].[{spMethodName}] "; for (int k = 0; k < declationStatmentList.Count; k++) { spExecuteParamter += $"@{spMethodParamters.Parameters[k].Identifier}={{{k}}}"; if (k < declationStatmentList.Count - 1) { spExecuteParamter += ","; } } spExecuteParamter += $";\""; for (int k = 0; k < declationStatmentList.Count; k++) { spExecuteParamter += $",{declationStatmentList[k].Declaration.Variables.First().Identifier.Text}"; } spExecuteParamter += $");\nvar queryobject = await query.FirstOrDefaultAsync();\n" + $"result = ({spReturnType})queryobject;" + "}\n catch (Exception ex)\n{\nthrow ex;\n}\n return" + $" result;\n" + "}\n"; allModifiedStoredProcdures += firstsection + spDeclationStatmentList + Sp_ExecuteStatment + spExecuteParamter; } } // Add allModelBuilderQueries var coreNewOnModelCreatingMethod = coreOldOnModelCreatingMethod.AddBodyStatements(ConvertToSyntaxStatment(allModelBuilderQuery)); // to delete OldOnModelCreatingMethod from class object coreRoot.RemoveNode(coreOldOnModelCreatingMethod, new SyntaxRemoveOptions()); // to Add NewOnModelCreatingMethod in Class var coreContextclassAfterAddingBuilderQueries = Core_contextclass.AddMembers(coreNewOnModelCreatingMethod); //Add All Stored Procedures to class Context var coreNewContextClass = coreContextclassAfterAddingBuilderQueries.AddMembers(CSharpSyntaxTree .ParseText(allModifiedStoredProcdures).GetCompilationUnitRoot().Members.ToArray()); //Replace old Context class With new Class Context object coreRoot = coreRoot.ReplaceNode(Core_contextclass, coreNewContextClass); // Using Statments string selectedProviderNameSpace = db_helper.GetNamespaceProvider(cmb_DBProvider.Text); // Add New Using Namespaces coreRoot = coreRoot.AddUsings( ConvertToUsingDirectiveArray(new string[] { "System.Collections.Generic", "System.Threading.Tasks", "System.Linq", selectedProviderNameSpace })); txt_Target.Text = coreRoot.ToFullString(); } catch (Exception ex) { MessageBox.Show(ex.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error); } }