1

這是我的第一個asp.net mvc項目。我用EF代碼第一個的方法創建了一個單獨的類庫來編寫我的模型和上下文。我也參考了mvc項目中的類庫。 但是現在在我嘗試啓用遷移時創建了更多的新模型後,它給了我一個錯誤。 我也試過這裏提到的所有可能的解決方案:No context type found in the assembly. ASP.NET MVC4ASP.NET MVC:程序集中找不到上下文類型

仍然存在問題。 我錯過了什麼嗎?

Migration Error 這是上下文類代碼:

using System; 
 
using System.Collections.Generic; 
 
using System.Data.Entity; 
 
using System.Linq; 
 
using System.Text; 
 
using System.Threading.Tasks; 
 
using System.Data.Entity.ModelConfiguration.Conventions; 
 
using FYPPharmAssistant.Domain.Models; 
 

 

 
namespace FYPPharmAssistant.Domain.DAL 
 
{ 
 
    public class MyContext : DbContext 
 
    { 
 
     public MyContext() 
 
      : base("MyConnectionString") 
 
     { 
 

 
     } 
 
     public DbSet<GenericName> GenericNames { get; set; } 
 
     public DbSet<Item> Items { get; set; } 
 
     public DbSet<Manufacturer> Manufacturers { get; set; } 
 
     public DbSet<Stock> Stocks { get; set; } 
 
     public DbSet<PaymentStatus> PaymentStatus { get; set; } 
 
     public DbSet<Purchase> Purchase { get; set; } 
 
     public DbSet<PurchaseItem> PurchaseItems { get; set; } 
 
     public DbSet<Supplier> Suppliers { get; set; } 
 

 
     //avoids pluralizing table names 
 
     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
 
     { 
 
      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
 
     } 
 
    } 
 
}

而且我的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="MyConnectionString" connectionString="Data Source=AVISHEKH\SQLEXPRESS; Initial Catalog= FYPPharmAssistant; Integrated Security=SSPI;" 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> 
 
    <compilation debug="true" targetFramework="4.5" /> 
 
    <httpRuntime targetFramework="4.5" /> 
 
    </system.web> 
 
    <runtime> 
 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
 
     </dependentAssembly> 
 
     <dependentAssembly> 
 
     <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" /> 
 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.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="1.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.WebPages" 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.2.0" newVersion="5.2.2.0" /> 
 
     </dependentAssembly> 
 
    </assemblyBinding> 
 
    </runtime> 
 
    <entityFramework> 
 
    <!-- 
 
    <contexts> 
 
     <context type="FYPPharmAssistant.Domain.DAL.MyContext, FYPPharmAssistant"> 
 
     <databaseInitializer type="FYPPharmAssistant.Domain.DAL.PharmacyInitializer, FYPPharmAssistant" /> 
 
     </context> 
 
    </contexts> --> 
 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
 
    <providers> 
 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
 
    </providers> 
 
    </entityFramework> 
 
</configuration>

+0

你嘗試在你的網頁更改名稱=「MyConnectionString」來命名=「MyContext」的.config? – DDiVita

+0

是的,我做到了。但它不能解決問題。 –

+0

http://prntscr.com/89xplq是否在正確的項目上執行遷移啓用? btw由第二次嘗試顯示的消息我認爲正確的方法來做到這一點將啓用遷移-ContextTypeName Domain.Dal.MyContext –

回答

1

這可能對未來某個人有所幫助。所以我再次寫它。

我只是改變默認的項目到FYPPharmacyAssistant.Domain從下拉列表 在PackageManager控制檯和問題 解決。 FYPPharmacyAssistant.Domain是我的單獨項目或 類庫,由我的所有實體和數據庫上下文 類組成。

enter image description here

enter image description here

0

語境需要在同一個項目作爲遷移的配置

+0

請更具體。包括關於要更改哪些代碼的具體建議。 –

相關問題