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

前端 未结 2 1486
走了就别回头了
走了就别回头了 2021-01-26 01:02

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 co

相关标签:
2条回答
  • 2021-01-26 01:13

    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();
        }
    }
    
    0 讨论(0)
  • 2021-01-26 01:25

    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" />
    
    0 讨论(0)
提交回复
热议问题