c# - Entity Framework 6 迁移与 MVC 6 RC 1

标签 c# asp.net-mvc entity-framework entity-framework-6 asp.net-core-mvc

我们在单独的类库项目(数据层)中有一个 EF 6 Dbcontext 和迁移,而不是在主 mvc 6 项目中。 然而,当我们试图在项目的第一次运行时调用它

context.Database.Initialize(false);

我们收到非常奇怪的错误信息

System.IO.FileNotFoundException Could not find file 'MyProject.Data.resources'.

堆栈跟踪:

at System.Reflection.RuntimeAssembly.InternalGetSatelliteAssembly(String name, CultureInfo culture, Version version, Boolean throwOnFileNotFound, StackCrawlMark& stackMark) 
at System.Resources.ManifestBasedResourceGroveler.GetSatelliteAssembly(CultureInfo lookForCulture, StackCrawlMark& stackMark) 
at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary<String, ResourceSet> localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark) 
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark) 
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) 
at System.Resources.ResourceManager.GetString(String name, CultureInfo culture) 
at System.Resources.ResourceManager.GetString(String name) 
at System.Data.Entity.Migrations.DbMigrator.GetDefaultSchema(DbMigration migration) 
at System.Data.Entity.Migrations.DbMigrator.<GetHistorySchemas>b__6(<>f__AnonymousType10<String, DbMigration> <>h__TransparentIdentifier4) 
at System.Linq.Enumerable.<>c__DisplayClass7_0<TSource, TMiddle, TResult>.<CombineSelectors>b__0(TSource x) 
at System.Linq.Enumerable.WhereSelectListIterator<TSource, TResult>.MoveNext() 
at System.Linq.Enumerable.<ConcatIterator>d__58<TSource>.MoveNext() 
at System.Linq.Enumerable.<ConcatIterator>d__58<TSource>.MoveNext() 
at System.Linq.Enumerable.<DistinctIterator>d__63<TSource>.MoveNext() 
at System.Linq.Buffer<TElement>..ctor(IEnumerable<TElement> source) 
at System.Linq.Enumerable.<ReverseIterator>d__74<TSource>.MoveNext() 
at System.Data.Entity.Migrations.History.HistoryRepository.QueryExists(String contextKey) 
at System.Data.Entity.Migrations.History.HistoryRepository.Exists(String contextKey) 
at System.Data.Entity.Migrations.History.HistoryRepository.<GetUpgradeOperations>d__16.MoveNext() 
at System.Linq.Enumerable.Any<TSource>(IEnumerable<TSource> source) 
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) 
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b() 
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) 
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) 
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) 
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() 
at System.Data.Entity.MigrateDatabaseToLatestVersion<TContext, TMigrationsConfiguration>.InitializeDatabase(TContext context) 
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf<TContext>.<CreateInitializationAction>b__e() 
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) 
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() 
at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) 
at System.Data.Entity.Internal.RetryAction<TInput>.PerformAction(TInput input) 
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action<InternalContext> action) 
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() 
at System.Data.Entity.Internal.InternalContext.Initialize() 
at System.Data.Entity.Database.Initialize(Boolean force) 
at MyProject.Data.DbHelper.SetupDatabase() in E:\Projects\MyProject\src\MyProject.Data\DbHelper.cs:line 29 
at MyProject.Business.Services.DatabaseService.SetupDatabase() in E:\Projects\MyProject\src\MyProject.Business\Services\DatabaseService.cs:line 17 
at MyProject.Web.Startup.ConfigureServices(IServiceCollection services) in E:\Projects\MyProject\src\MyProject.Web\Startup.cs:line 61

这很奇怪。当我们尝试在数据项目的 App.config 中设置连接字符串并从包管理器控制台运行更新数据库时,它会起作用(但我相信那是因为在这种情况下它的行为就像一个单独的项目)。

谁有解决这个问题的好方法?

附言其他问题没有回答这个问题,因为这是从 xproj 项目访问 csproj 项目中的迁移的问题。

最佳答案

这是一个已知的 dnx 问题。这不是迁移问题,而是类库中 EF6 模型的问题。

答案在这里:Entity Framework 7 error : "EntityFramework.Core.resources" is missing (7.0.0-rc1-final)

将此添加到您的 Startup 的 Configure 方法中:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        //...
        var localizationOptions = new RequestLocalizationOptions()
        {
            SupportedCultures = new List<CultureInfo> { new CultureInfo("") },
            SupportedUICultures = new List<CultureInfo> { new CultureInfo("") }
        };

        var invariantCulture = new RequestCulture(new CultureInfo(""), new CultureInfo(""));

        app.UseRequestLocalization(localizationOptions, invariantCulture);
        //...
    }

关于c# - Entity Framework 6 迁移与 MVC 6 RC 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35194255/

相关文章:

c# - 从字符串定义的表名中获取根据字符串定义的字段过滤的数据

c# - Pechkin 和 IIS 中使用的进程

asp.net-mvc - 使用依赖注入(inject)在 AspNetCore Mvc 中配置 CookieAuthenticationOptions

c# - Entity-Framework中自动查询结果

c# - 使用 Linq 语句从 intellisense 中选择缺少的方法

entity-framework - 使用 Entity Framework Core 执行原始 sql

c# - 如何传递一个以上的字节

c# - 文本解码问题

c# - 在 ASP.NET 中创建验证码的代码?

c# - 使用 EF6+MVC5 渲染网页时性能不佳。为什么?