4

Edit 2:

This does not happen when I use SQL Server. It only happen when I use Oracle. There has been a suggestion to debug or to provide inner exception for this. Yet, since this exception happen during the scaffolding, I can't get the inner exception. Also, I am not sure if we can debug a scaffolding process. If there is anybody who knows how it can be done, please let me know.


Error

There was an error running the selected code generator:

'Exception has been thrown by the target of an invocation'

Ok, firstly, the search results for this error seems to return many links.

And thus I learned that this error is not exclusive to creating Controller scaffolding in EF 6.

But my case is when I am about to create Controller Scaffolding using EF 6 in VS2013, when I create MVC web application.

enter image description here

The option I use is:

MVC 5 Controller with views, using Entity Framework

I use Oracle Database and Oracle.ManagedDataAccess namespace. Some relevant posts I found are these:

But none of them talking about the specific case for Oracle DB

But since they are talking about any DB in general, I nevertheless try some of their solutions, including:

  1. Removing all sections, connectionStrings, and providers such that each of them only contain single item:

     <configSections>
       <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
     </configSections>
    
     <connectionStrings>
       <add name="EmployeeContext" connectionString="metadata=res://*/Models.EmployeeDataModel.csdl|res://*/Models.EmployeeDataModel.ssdl|res://*/Models.EmployeeDataModel.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string=&quot;DATA SOURCE=VSDB;PASSWORD=mypassword;USER ID=myuser&quot;" providerName="System.Data.EntityClient" />
     </connectionStrings>
    
     <providers>
       <provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
     </providers>
    
  2. Change DbSet in the Context to IDBSet

     public virtual DbSet<Employee> Employees1 { get; set; }
    
  3. Use VS2013 with update 5.

  4. Upgrade to Entity Framework 6.1.3

  5. Changing the defaultConnectionFactory from:

     <entityFramework>
       <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
         <parameters>
             <parameter value="mssqllocaldb" />
         </parameters>
       </defaultConnectionFactory>
     </entityFramework>
    

    to

     <entityFramework>
       <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
         <parameters>
             <parameter value="v11.0" />
         </parameters>
       </defaultConnectionFactory>
     </entityFramework>
    
  6. Change OnModelCreating event handler from:

     protected override void OnModelCreating(DbModelBuilder modelBuilder) {
         throw new UnintentionalCodeFirstException();
     }
    

    to

     protected override void OnModelCreating(DbModelBuilder modelBuilder) {
         base.OnModelCreating(modelBuilder);
         modelBuilder.HasDefaultSchema("myschema");
     }
    

But none of those works. I still get the same error. I even try all of them together and the problem still persists.

I have no more idea on what should be done in this case. Any idea?

Edit:

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. I am new with both web programming and Entity framework and really have no idea on what is going on behind the scene which causes the error.

Edit 2:

I have tried to isolate the problem by trying to redo the scaffolding by SQL server as suggested by Steve in the comment and Squiggle in the chat and I got no issue at all. So, the problem must have something to do with Oracle DB settings or (maybe) with the ODP.Net tool which I use Oracle.ManagedDataAccess - whether this is supported by EF.

Community
  • 1
  • 1
Ian
  • 30,182
  • 19
  • 69
  • 107
  • Is there any other message or inner exception on the error? Target of an invocation is extremely broad (as you've found). – Steve Feb 23 '16 at 01:54
  • @Steve the thing is, I cannot debug the inner exception since it is not a code - but a scaffolding, gotten from creating Controller by `MVC 5 Controller with views, using Entity Framework`. If there is any way to debug the inner exception for scaffolding, I would like to know too.. – Ian Feb 23 '16 at 01:56
  • So you get it when you *create* the controller using Visual Studio tools? Hmm...Obvious question, but have you tried it in a new solution, or a different database access type? – Steve Feb 23 '16 at 01:58
  • @Steve "when you create the controller using Visual Studio tools" yes, it is as you say... If you know how to debug such case, I would like to know how. I do it following a lecture shown in the youtube video series: https://www.youtube.com/watch?v=-pzwRwYlXMw&list=PL6n9fhu94yhVm6S8I2xd6nYz2ZORd7X2v at least in the video, there is not problem when using `SqlClient` default database. As for using `Oracle.ManagedDataAccess`, I have to do some configuration to make it work, and now I am stuck in this issue - not sure what configuration I left out. – Ian Feb 23 '16 at 02:02
  • Perhaps you can try to comment out some code in your data context class, e.g. the dbset properties and check whether at some point the error disappears. – Martin Feb 25 '16 at 15:25
  • @Martin thanks for the suggestion. I have more or less done it as I try to configure my project but to no avail. Nevertheless, I will try and see again. – Ian Feb 25 '16 at 15:57

3 Answers3

3

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 Offin 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.

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
1

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:

  1. EF 6 from Nuget
  2. Oracle.ManagedDataAccess v12.2.1100
  3. 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> -->
LarsTech
  • 80,625
  • 14
  • 153
  • 225
luciano
  • 19
  • 4
0

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.

Vince
  • 1