6

I have EF 5.0 code-first VS 2012 project and all Entity Framework menu commands (View Entity Data Model DDL SQL) produce "Exception has been thrown by the target of an invocation" popup. I think what has also changed is that EF Power Tools Beta 1 (or VS 2010, I am not sure) use to display EF Power Tools messages in the output window. Now all I get is the popup... Is this VS or Power Tools issue?

jm.
  • 23,422
  • 22
  • 79
  • 93
Stan Bashtavenko
  • 1,025
  • 11
  • 16
  • 1
    i am having the same problem did you find out why or how to resolve it? – Eatdoku Oct 06 '12 at 18:03
  • I believe this is the EF Power Tools issue. The biggest problem here is that VS does not give enough details to figure out what is wrong... – Pawel Oct 08 '12 at 17:18
  • That's exactly my point -- there's nothing to help narrowing it down. It can be a), b) c).. I don't want to go through all possible combinations.. the previous beta at least displayed the progress and messages, but the new one does not (or maybe I don't know where to look..) – Stan Bashtavenko Oct 19 '12 at 18:07

5 Answers5

4

this is my work around:

Comment the constructor out, and leave the static MyDbContext as is -->

public class MyDbContext: DbContext
{
    public static string ConnectionName = "Name = SMS_ADvTECHContext";
    static MyDbContext()
    {
        Database.SetInitializer<SMS_ADvTECHContext>(null);
    }

/*  public SMS_MyDbContext()
        : base(ConnectionName)
    {
    }*/
}

Then if you right click the context class --> Enityframework --> View Entity Data Model (read-only) it generate the view!

1

I ran into this error when I didn't have the correct default connection factory configured in the App.config inside the project that included my DbContext class. I updated it to use the correct factory and this error went away. In my case I set it to use the LocalDbConnectionFactory:

 <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameters>
            <parameter value="v11.0" />
        </parameters>
    </defaultConnectionFactory>
</entityFramework>
Rafe
  • 8,467
  • 8
  • 47
  • 67
1

In ran into this error and it was an even simpler issue ... the project that contained my Context was not the Startup Project. Once I set the project to be the Startup Project it started working.

Gary O. Stenstrom
  • 2,284
  • 9
  • 38
  • 59
0

Maybe Visual Studio is having trouble figuring out what connection string to use for your DBContext, when you choose the Entity Framework menu commands.

In my case, I was able to resolve this by verifying that I had a "default" connection string for my dbContext. So that, when you right click on db context and choose Entity framework, you will have a connection to the DB.

In other words, I had modified my DBContext to select the connection string from a command line parameter to my app. So, normally, my db context did not have a "default" value.

public class MyDbContext : DbContext
{
    public static string ConnectionName; 

    public DnnDbContext()
        : base( "Name=" + ConnectionName)  {     
    }

As you can see, I had no ConnectionString by default.

I changed to:

    public static string ConnectionName = "DefaultConnNameInAppConfig"; 
jm.
  • 23,422
  • 22
  • 79
  • 93
0

I ran into this when I had multiple connection strings with the same name configured in my web.config.

Bravax
  • 10,453
  • 7
  • 40
  • 68