How to Debug and to Fix 'Exception has been thrown by the target of an invocation' for EF 6, Oracle DB, Controller Scaffolding
I am not sure about the real issue in scaffolding but I want to add help to
"I would also be happy to know if someone can tell me how can I "debug" what's wrong with the scaffolding for my case to get the right solution for my case. "
Every .net web project has single entry point Global.asax
that can handle all unhandled exceptions.
Make sure you don't have customErrors
to Off
in Web.config
files.
Just add Application_Error method in Global.asax like:
void Application_Error(object sender, EventArgs e){ ... }
Better implementation is available at MSDN Link:
void Application_Error(object sender, EventArgs e){ // Code that runs when an unhandled error occurs // Get the exception object. Exception exc = Server.GetLastError(); // Handle HTTP errors if (exc.GetType() == typeof(HttpException)) { // The Complete Error Handling Example generates // some errors using URLs with "NoCatch" in them; // ignore these here to simulate what would happen // if a global.asax handler were not implemented. if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength")) return; //Redirect HTTP errors to HttpError page Server.Transfer("HttpErrorPage.aspx"); } // For other kinds of errors give the user some information // but stay on the default page Response.Write("<h2>Global Page Error</h2>\n"); Response.Write( "<p>" + exc.Message + "</p>\n"); Response.Write("Return to the <a href='Default.aspx'>" + "Default Page</a>\n"); // Log the exception and notify system operators ExceptionUtility.LogException(exc, "DefaultPage"); ExceptionUtility.NotifySystemOps(exc); // Clear the error from the server Server.ClearError();}
You can catch/watch unhandled exception here with all inner exceptions here.Hope it help in debugging the issue.
The most errors occurs due a bad configuration, for instance: connection strings, providers and so on.
In my case figured out that the issue was caused because when installed Oracle client, I clicked to configure it on machine.config so the configuration was taken from there.
Therefore, I deleted the oracle configuration from machine.config , after that the scaffolding working as we expected.
Installed:
- EF 6 from Nuget
- Oracle.ManagedDataAccess v12.2.1100
- Oracle.ManagedDataAccess.EntiyFramework v12.2.1100
Removed from machine.config:
<!-- <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" /> --><!-- <oracle.manageddataaccess.client> --> <!-- <version number="4.122.1.0"> --> <!-- <settings> --> <!-- <setting name="tns_admin" value="c:\oracle64\client\laralal\product\12.2.0\client_1\network\admin" /> --> <!-- </settings> --> <!-- </version> --> <!-- </oracle.manageddataaccess.client> -->
I was getting this exact error attempting to scaffold using my oracle db but had no problem scaffolding to my SQL db. What I found was there was a version # discrepancy in the oracle.manageddataaccess.client sections of the machine.config file versus the web.config file. So I commented out every section in the machine.config file that contained "oracle.manageddataaccess.client", rebuilt my application and I was able to scaffold my controller and views successfully.