Paanshul Dobriyal
Paanshul Dobriyal

Reputation: 1

Exception has been thrown by the target of an invocation- while creating the controller

I am a beginner in ASP.NET MVC 5 and I was building a small application. While I was adding a controller a message popped up saying: "There was an error running the selected code generator: Exception has been thrown by the target of an invocation". Before adding the controller I added a connection string in the Web.Config file. Please tell me how to resolve this error.

Upvotes: 0

Views: 3705

Answers (3)

Faisal Shahid
Faisal Shahid

Reputation: 3

I had two connections string with same model but one was with empty "name" and the other one was with the using connectionstring. i remove the empty "name" connectionstring and my issue has resolved.

Upvotes: 0

Teppic
Teppic

Reputation: 2546

Make sure you set throwIfV1Schema to false in the constructor of your DbContext, e.g.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

Upvotes: 0

emdub
emdub

Reputation: 21

There could be a discrepancy between your model and connection string. Have you created a model? If so, then your DbContext class name must match the one specified in your connection string. It's also case sensitive. See the example below.

---Model class---

Public Class Model
    Public Property ...
End Class

Public Class ModelDbContext
    Inherits DbContext

    Public Property Models As DbSet(Of Model)
End Class

Make sure to import Imports System.Data.Entity

Your connection string should be something like this:

<add name="ModelDbContext"
     connectionString="Data Source=LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Models.mdf;Integrated Security=True"
     providerName="System.Data.SqlClient" />

Upvotes: 2

Related Questions