2016-09-10 142 views
0

我正在開發我的第一個MVC項目,以及由Andrew Troelsen和Philip Japikse撰寫的C#6.0和.NET 4.6框架。 在我的解決方案有兩個項目,一個是MVC應用程序,另一個用於數據訪問圖書館,使用實體框架添加控制器不起作用?

當我添加與視圖的新控制器,採用EF 我得到這個錯誤:

enter image description here

there was an error running the selected code generator: exception has been thrown by the target of an invocation

有在這個問題上的許多問題,我想盡一切可能的解決方案:

  • 我感動的精讀nectionString到一個分離的配置文件
  • 我清除緩存文件夾
  • 我沒有OnModelCreating在上下文類
  • 我有兩個項目上安裝EF的同一版本,他們使用.NET 4.6兩種 0.1。

用於測試目的,我創造了DAL項目中的新EF模塊(創建實體類和背景類),當我加了一個控制器我得到了一個錯誤(消息框錯誤)說,它」 t在應用程序配置中找到連接字符串(我是新手到MVC)我將它從web.config移到了app.config中,我得到了同樣的錯誤,我將它移回,保留在兩個配置文件,它的工作原理和腳手架成功完成(我不明白爲什麼我應該在app.config和web.config文件中保留相同的連接字符串!)。 我修訂我模塊的連接字符串,這將導致錯誤,並沒有什麼不同,我注意到:

這裏是App.config中:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
    <connectionStrings> 
    <add name="AutoLotConnection" connectionString="data source=.;initial catalog=AutoLotEF;integrated security=True; 
     MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> 
    <add name="MyCon" connectionString="data source=.; initial catalog= AnotherDB; integrated security=true; 
     MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/> 
    </connectionStrings> 
</configuration> 

的web.config

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=301880 
    --> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <connectionStrings> 
    <add name="DefaultConnection" connectionString="Data Source=.;AttachDbFilename=|DataDirectory|\aspnet-MVCTest-20160910010148.mdf;Initial Catalog=aspnet-MVCTest-20160910010148;Integrated Security=True" providerName="System.Data.SqlClient" /> 
     <add name="MyCon" connectionString="data source=.; initial catalog= AnotherDB; integrated security=true; 
     MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/> 
    <add name="AutoLotConnection" connectionString="data source=.;initial catalog=AutoLotEF;integrated security=True; 
     MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> 

    <add name="ModelTest" connectionString="data source=(LocalDb)\MSSQLLocalDB;initial catalog=MVCTest.ModelTest;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /></connectionStrings> 
    <appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 
    <authentication mode="None" /> 
    <compilation debug="true" targetFramework="4.6.1" /> 
    <httpRuntime targetFramework="4.6.1" /> 
    <httpModules> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
    </httpModules> 
    </system.web> 
    <system.webServer> 
    <modules> 
     <remove name="FormsAuthentication" /> 
     <remove name="ApplicationInsightsWebTracking" /> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 
    </modules> 
    <validation validateIntegratedModeConfiguration="false" /> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
</configuration> 

這是型號的庫存分類:

[Table("Inventory")] 
    public partial class Inventory 
    { 
     [Key] 
     public int CarId { get; set; } 
     [StringLength(50)] 
     public string Make { get; set; } 
     [StringLength(50)] 
     public string Color { get; set; } 
     [StringLength(50)] 
     public string PetName { get; set; } 
     [Timestamp] 
     public byte[] Timestamp { get; set; } 

     public virtual ICollection<Order> Orders { get; set; } = new HashSet<Order>(); 
    } 

上下文類:

public class AutoLotEntities : DbContext 
    { 
     static readonly DatabaseLogger DatabaseLogger = new DatabaseLogger("sqllog.txt", true); 
     public AutoLotEntities() 
      : base("name=AutoLotConnection") 
     { 
      //DbInterception.Add(new ConsoleWriterInterceptor()); 
      DatabaseLogger.StartLogging(); 
      DbInterception.Add(DatabaseLogger); 
     } 
     public virtual DbSet<CreditRisk> CreditRisks { get; set; } 
     public virtual DbSet<Customer> Customers { get; set; } 
     public virtual DbSet<Inventory> Inventory { get; set; } 
     public virtual DbSet<Order> Orders { get; set; } 
    } 
+0

你檢查這http://stackoverflow.com/questions/23576294/application-cant-scaffold-items – OctoCode

+0

是的,不工作 –

回答

0

我註釋掉DatabaseLogger線路中的上下文類,這解決了這個問題!

//static readonly DatabaseLogger DatabaseLogger = new 
DatabaseLogger("sqllog.txt", true); 

     public AutoLotEntities() 
      : base("name=AutoLotConnection") 
     { 
      //DbInterception.Add(new ConsoleWriterInterceptor()); 
      //DatabaseLogger.StartLogging(); 
      //DbInterception.Add(DatabaseLogger); 
     } 
相關問題