这是我第一次的 asp MVC 项目。我用的 EF code首先方法,并建立了一个单独的类库,以写我的模型和背景。我还引用了类库中MVC项目。但现在创建几个新车型,当我尝试启用迁移后,它给了我一个错误。我也尝试了所有在这里提到的可能的解决方案:No在装配中的上下文类型。 ASP.NET MVC4
静止问题仍然是相同的。我缺少的东西吗?
这是code的上下文类:
\r\r
使用系统;\r使用System.Collections.Generic;\r使用System.Data.Entity的;\r使用System.Linq的;\r使用System.Text;\r使用System.Threading.Tasks;\r使用System.Data.Entity.ModelConfiguration.Conventions;\r使用FYPPharmAssistant.Domain.Models;\r\r\r命名空间FYPPharmAssistant.Domain.DAL\r{\r 公共类MyContext:的DbContext\r {\r 公共MyContext()\r :基地(MyConnectionString)\r {\r\r }\r 公共DbSet< GenericName> GenericNames {搞定;组; }\r 公共DbSet<项目>项目{搞定;组; }\r 公共DbSet<制造商和GT;制造商{搞定;组; }\r 公共DbSet<股票及GT;股票{搞定;组; }\r 公共DbSet< PaymentStatus> PaymentStatus {搞定;组; }\r 公共DbSet<购买>购买{搞定;组; }\r 公共DbSet<&购买项目GT; PurchaseItems {搞定;组; }\r 公共DbSet<供应商>供应商{搞定;组; }\r\r //避免复数表名\r 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)\r {\r modelBuilder.Conventions.Remove< PluralizingTableNameConvention>();\r }\r }\r}\r\r\r
和我的的web.config :\r\r
<?XML版本=1.0编码=UTF-8? >\r<! - \r 有关如何配置ASP.NET应用程序的更多信息,请访问:\r go.microsoft/fwlink/?LinkId=301880\r - >\r<结构>\r < configSections>\r <! - 有关Entity Framework的配置的详细信息,请访问go.microsoft/fwlink/?LinkID=237468 - >\r <节名称=的EntityFrameworkTYPE =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,的EntityFramework,版本6.0.0.0 =文化=中性公钥= b77a5c561934e089requirePermission =FALSE/>\r < / configSections>\r <&是connectionStrings GT;\r <添加名称=MyConnectionString的connectionString =数据源= AVISHEKH \\ SQLEX $ P $干燥综合征;初始目录= FYPPharmAssistant;集成安全性= SSPI;的providerName =System.Data.SqlClient的/>\r < /&是connectionStrings GT;\r <&的appSettings GT;\r <添加键=网页:版本值=3.0.0.0/>\r <添加键=网页:启用VALUE =FALSE/>\r <添加键=ClientValidationEnabledVALUE =真/>\r <添加键=UnobtrusiveJavaScriptEnabledVALUE =真/>\r < /的appSettings>\r <&的System.Web GT;\r <编译调试=真targetFramework =4.5/>\r <的httpRuntime targetFramework =4.5/>\r < /system.web>\r <&运行GT;\r < assemblyBinding的xmlns =瓮:架构 - 微软COM:asm.v1>\r < dependentAssembly>\r < assemblyIdentity名称=Microsoft.Owin公钥=31bf3856ad364e35/>\r < bindingRedirect oldVersion =1.0.0.0-3.0.0.0NEWVERSION =3.0.0.0/>\r < / dependentAssembly>\r < dependentAssembly>\r < assemblyIdentity名称=Microsoft.Owin.Security.OAuth公钥=31bf3856ad364e35/>\r < bindingRedirect oldVersion =1.0.0.0-3.0.0.0NEWVERSION =3.0.0.0/>\r < / dependentAssembly>\r < dependentAssembly>\r < assemblyIdentity名称=Microsoft.Owin.Security.Cookies公钥=31bf3856ad364e35/>\r < bindingRedirect oldVersion =1.0.0.0-3.0.0.0NEWVERSION =3.0.0.0/>\r < / dependentAssembly>\r < dependentAssembly>\r < assemblyIdentity名称=Microsoft.Owin.Security公钥=31bf3856ad364e35/>\r < bindingRedirect oldVersion =1.0.0.0-3.0.0.0NEWVERSION =3.0.0.0/>\r < / dependentAssembly>\r < dependentAssembly>\r < assemblyIdentity名称=Newtonsoft.Json文化=中性公钥=30ad4fe6b2a6aeed/>\r < bindingRedirect oldVersion =0.0.0.0-6.0.0.0NEWVERSION =6.0.0.0/>\r < / dependentAssembly>\r < dependentAssembly>\r < assemblyIdentity名称=System.Web.Optimization公钥=31bf3856ad364e35/>\r < bindingRedirect oldVersion =1.0.0.0-1.1.0.0NEWVERSION =1.1.0.0/>\r < / dependentAssembly>\r < dependentAssembly>\r < assemblyIdentity名称=WebGrease公钥=31bf3856ad364e35/>\r < bindingRedirect oldVersion =1.0.0.0-1.5.2.14234NEWVERSION =1.5.2.14234/>\r < / dependentAssembly>\r < dependentAssembly>\r < assemblyIdentity名称=System.Web.Helpers公钥=31bf3856ad364e35/>\r < bindingRedirect oldVersion =1.0.0.0-3.0.0.0NEWVERSION =3.0.0.0/>\r < / dependentAssembly>\r < dependentAssembly>\r < assemblyIdentity名称=System.Web.WebPages公钥=31bf3856ad364e35/>\r < bindingRedirect oldVersion =1.0.0.0-3.0.0.0NEWVERSION =3.0.0.0/>\r < / dependentAssembly>\r < dependentAssembly>\r < assemblyIdentity名称=System.Web.Mvc公钥=31bf3856ad364e35/>\r < bindingRedirect oldVersion =1.0.0.0-5.2.2.0NEWVERSION =5.2.2.0/>\r < / dependentAssembly>\r < / assemblyBinding>\r < /运行>\r <&的EntityFramework GT;\r <! - \r <&上下文GT;\r <上下文类型=FYPPharmAssistant.Domain.DAL.MyContext,FYPPharmAssistant>\r < databaseInitializer TYPE =FYPPharmAssistant.Domain.DAL.PharmacyInitializer,FYPPharmAssistant/>\r < /背景>\r < /背景> - >\r < defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.SqlConnectionFactory,的EntityFramework/>\r <供应商>\r <供应商invariantName =System.Data.SqlClient的TYPE =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>\r < /供应商>\r < /&的EntityFramework GT;\r< /结构>\r\r\r
解决方案这可能是有人在将来有所帮助。于是我又写了。
我的更改默认项目以FYPPharmacyAssistant.Domain 从软件包管理系统控制台的下拉列表并问题 就解决了。 FYPPharmacyAssistant.Domain是我的独立项目或 类库包括我的所有实体和数据库上下文 类。
This is my first asp mvc project. I used EF code first approach and created a separate class library to write my model and context. I also referenced the class library in mvc project. But now after creating few more new models when I try to enable migration, it gives me an error. I also tried all the possible solutions mentioned here : No context type found in the assembly. ASP.NET MVC4
Still problem remains the same. Am I missing something?
This is code for Context Class:
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>(); } } }
And my web.config:
<?xml version="1.0" encoding="utf-8"?> <!-- For more information on how to configure your ASP.NET application, please visit go.microsoft/fwlink/?LinkId=301880 --> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit go.microsoft/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>
解决方案
This might be helpful for someone in future. So I am writing it again.
I just changed the Default Project to FYPPharmacyAssistant.Domain from the dropdown list in PackageManager Console and the problem is solved. FYPPharmacyAssistant.Domain is my seperate project or class library consisting of all my entities and database context class.
更多推荐
ASP.NET MVC:上下文类型组装未发现
发布评论