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.
Asked
Active
Viewed 3,625 times
0
-
Can you please post the code that gives you the error? – TimSPQR Jul 13 '14 at 16:55
3 Answers
2
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" />

Yuliam Chandra
- 14,494
- 12
- 52
- 67

emdub
- 21
- 5
0
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();
}
}

Teppic
- 2,506
- 20
- 26
0
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.

Faisal Shahid
- 3
- 2