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?
5 Answers
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!
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>
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";