2017-01-18 82 views
2

我使用的是Visual Studio Community 2015 v 14.0.25431.01 Update 3和MS .NET Framework v 4.6.91586以下this tutorial,但在嘗試搭建控制器時出現以下錯誤,如下所述:實體框架核心1.0不腳手架控制器和視圖

Error

我已經嘗試了所有建議的解決方案herehere,但無濟於事。是的,也嘗試(重新)構建項目。

這裏是我的項目的相關代碼。

學生型號:

using System; 
using System.Collections.Generic; 

namespace ContosoUniversity.Models 
{ 
    public class Student 
    { 
     public int ID { get; set; } 
     public string LastName { get; set; } 
     public string FirstMidName { get; set; } 
     public DateTime EnrollmentDate { get; set; } 

     public ICollection<Enrollment> Enrollments { get; set; } 
    } 
} 

SchoolContext:

public class SchoolContext : DbContext 
    { 
     public SchoolContext(DbContextOptions<SchoolContext> options) : base(options) 
     { 
     } 

     public DbSet<Course> Courses { get; set; } 
     public DbSet<Enrollment> Enrollments { get; set; } 
     public DbSet<Student> Students { get; set; } 

     protected override void OnModelCreating(ModelBuilder modelBuilder) 
     { 
      modelBuilder.Entity<Course>().ToTable("Course"); 
      modelBuilder.Entity<Enrollment>().ToTable("Enrollment"); 
      modelBuilder.Entity<Student>().ToTable("Student"); 
     } 

    } 

添加了SchoolContext到服務startup.cs:

public void ConfigureServices(IServiceCollection services) 
     { 
      // Add framework services. 
      services.AddApplicationInsightsTelemetry(Configuration); 

      services.AddDbContext<SchoolContext>(options => 
       options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 

這是我在appSetting.json的ConnectionString :

"ConnectionStrings": { 
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-ContosoUniversity-75b88406-9d10-4237-814a-3f725b9b4e3d;Trusted_Connection=True;MultipleActiveResultSets=true" 
    } 

回答

1

好吧,所以我設法克服了沒有無參數構造函數的問題。至於建議here,我只是提供了構造到兩個的DbContext類以及Model類這樣:

public Student() { } 

現在我得到一個不同的錯誤。我想我需要把它作爲一個新問題。我會的,但爲什麼它的價值,這是錯誤日誌的一部分:

 Attempting to figure out the EntityFramework metadata for the model and DbContext: Student 
    Exception has been thrown by the target of an invocation. StackTrace: 
     at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) 
     at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) 
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
     at Microsoft.AspNetCore.Hosting.Internal.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) 
     at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() 
     at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() 
     at Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.EntityFrameworkServices.TryCreateContextUsingAppCode(Type dbContextType, ModelType startupType) 
    A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.) StackTrace: 
... 
    System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) 
    There is no entity type Student on DbContext ContosoUniversity.Data.SchoolContext at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,... 
+0

事實證明,我的VS 2015的安裝有點損壞。重新安裝和腳手架工作完美 - 如果非常慢(與Rails相比)。 – PakiPat