ASP.NET 5 EntityFramework.Core 7.0.0-RC1-最后一个问题 - 编译器7.0.0.0要被引用未找到 [英] ASP.NET 5 EntityFramework.Core 7.0.0-rc1-final issue - Compiler wants 7.0.0.0 to be referenced which is not found

查看:694
本文介绍了ASP.NET 5 EntityFramework.Core 7.0.0-RC1-最后一个问题 - 编译器7.0.0.0要被引用未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有同样的问题。我在 project.json 文件添加以下的依赖:

\r
\r

依赖:{\r
    的EntityFramework:7.0.0-BETA4\r
    EntityFramework.Core:7.0.0-RC1决赛\r
    EntityFramework.SqlServer:7.0.0-beta8,\r
    EntityFramework.Commands:7.0.0-RC1决赛\r
  },\r
  命令:{\r
    网络:Microsoft.AspNet.Server.Kestrel\r
    EF:EntityFramework.Commands\r
  },\r
...

\r

\r
\r

我用 DNU安装的EntityFramework DNU安装EntityFramework.SqlServer 来安装软件包。 的EntityFramework:由安装程序自动本身7.0.0-BETA4的依赖写节点<。 / p>

问题/问题1:令我吃惊,当我在intellisence拉动了的EntityFramework 可用的版本我是presenting与仅 6.1.3

问题2:当我使用编译我的应用程序 DNU建立(我加入后跑了 DNU恢复命令的EntityFramework 组件(。核心 .Commands )手动下<我收到了一堆编译错误的code>依赖节点:

\r
\r

类型'DbContextOptionsBuilder'在未引用的程序集的定义。您必须添加一个引用程序集EntityFramework.Core,版本= 7.0.0.0,文化=中立,公钥=空'。\r
D:\\项目\\ ASPNET \\ apiservice \\ \\型号SampleData.cs(12,41):DNXCore,版本V5.0 =错误CS0308:非泛型方法IServiceProvider.GetService(类型)不能与类型参数使用\r
D:\\项目\\ ASPNET \\ apiservice \\ \\型号SampleData.cs(13,32):DNXCore,版本V5.0 =错误CS0308:非泛型方法IServiceProvider.GetService(类型)不能与类型参数使用\r
D:\\项目\\ ASPNET \\ apiservice \\ \\型号SampleData.cs(14,29):DNXCore,版本V5.0 =错误CS1061:'对象'不包含'书',没有扩展方法的定义,图书接受类型'对象'的第一个参数可以找到(是否缺少using指令或程序集引用?)\r
D:\\项目\\ ASPNET \\ apiservice \\ \\型号SampleData.cs(15,42):DNXCore,版本V5.0 =错误CS1061:'对象'不包含'作者的定义,并没有扩展方法作者的接受类型'对象'的第一个参数可以找到(是否缺少using指令或程序集引用?)\r
D:\\项目\\ ASPNET \\ apiservice \\ \\型号SampleData.cs(17,43):DNXCore,版本V5.0 =错误CS1061:'对象'不包含'作者的定义,并没有扩展方法作者的接受类型'对象'的第一个参数可以找到(是否缺少using指令或程序集引用?)\r
D:\\项目\\ ASPNET \\ apiservice \\ \\型号SampleData.cs(19,45):DNXCore,版本V5.0 =错误CS1061:'对象'不包含'作者的定义,并没有扩展方法作者的接受类型'对象'的第一个参数可以找到(是否缺少using指令或程序集引用?)\r
D:\\项目\\ ASPNET \\ apiservice \\ \\型号SampleData.cs(22,29):DNXCore,版本V5.0 =错误CS1061:'对象'不包含'书',没有扩展方法的定义,图书接受类型'对象'的第一个参数可以找到(是否缺少using指令或程序集引用?)\r
D:\\项目\\ ASPNET \\ apiservice \\ \\型号SampleData.cs(61,29):DNXCore,版本V5.0 =错误CS1061:'对象'不包含'的SaveChanges的定义,并没有扩展方法'的SaveChanges接受类型'对象'的第一个参数可以找到(是否缺少using指令或程序集引用?)

\r

\r
\r

如果我删除。核心 .Commands 集的引用项目生成精细。

然后,我改变了版本 EntityFramework.Core 7.0.0.0 中提到的错误:

\r
\r

类型'DbContextOptionsBuilder'在未引用的程序集的定义。您必须添加一个引用程序集EntityFramework.Core,版本= 7.0.0.0,文化=中立,公钥=空'。

\r

\r
\r

DNU恢复现在给我的以下内容:

\r
\r

无法找到依赖EntityFramework.Core&GT; = 7.0.0\r
写锁文件D:\\项目\\ ASPNET \\ apiservice \\ project.lock.json\r
恢复完成后,经过6675ms\r
\r
在错误D:\\项目\\ ASPNET \\ apiservice \\ project.json\r
    无法找到依赖EntityFramework.Core&GT; = 7.0.0

\r

\r
\r

我在 asp.net 5 Visual Studio的code 平台的新手。我不能用的Visual Studio 2015年作为开发团队的其他成员正在使用OSX。

这对我来说是一个学习的项目。要求是pretty直线前进。连接SQL Server数据库,拉使用实体框架的主子相关数据。

秀逗!帮助!

在此先感谢

更新


  1. 我可以看到 project.json 的重大变化与版本的所有包 7.0.0-RC1决赛。我用约曼发电机工具来创建项目,但看起来你纠正通过的Visual Studio 包。所以,我必须手动更新包的版本和恢复呢?


  2. 在创建项目/由约曼产生的,有添加默认情况下没有 global.json 文件。我可以手动添加这,用手把code里面呢?将这项工作?


  3. 我还可以看到一些额外的软件包引用添加到 project.json 这不是由默认添加由约曼发电机文件 - 如 Microsoft.AspNet.Tooling.Razor Microsoft.Extensions.Logging.Console Microsoft.Extensions.Logging.Debug Microsoft.Extensions.Logging 等都是他们都被该项目有效地使用?由于发电机没有自动添加它们,再一次,我可以手动添加和使用 DNU恢复恢复?这会对项目产生任何影响,如果我手动添加?



解决方案

您应该的删除 的EntityFramework从依赖(删除行的EntityFramework:7.0.0-BETA4),并用

 EntityFramework.MicrosoftSqlServer:7.0.0-RC1决赛
EntityFramework.MicrosoftSqlServer.Design:7.0.0-RC1决赛
EntityFramework.Commands:7.0.0-RC1决赛

而已。你并不需要添加EntityFramework.Core:7.0.0-RC1决赛:如果你加入明确EntityFramework.MicrosoftSqlServer 7.0.0-RC1决赛,因为下面的依赖关系:

 EntityFramework.MicrosoftSqlServer:7.0.0-RC1决赛:
    EntityFramework.Relational:7.0.0-RC1决赛:
        EntityFramework.Core:7.0.0-RC1决赛

我的意思是 EntityFramework.MicrosoftSqlServer 要求 EntityFramework.Relational ,要求的EntityFramework。核心

您应该还验证 global.json 包含SDK:{版本:1.0.0-RC1-UPDATE1}

更新时间::我检查您上传的测试项目。它有很多小问题。我修改了它。您也可以下载修改的项目 这里 不要忘了修复的ConnectionString appsettings.json 使用,它包含服务器这是我在我所用。

最重要的变化是使用更简单 BookContext.cs

使用Microsoft.Data.Entity;命名空间apiservice.Models {
    公共类BookContext:{的DbContext
        公共DbSet&LT;作者&GT;作者{搞定;组; }
        公共DbSet&LT;图书&GT;图书{搞定;组; }
    }
}

的使用以下 SampleData.cs

使用系统;
使用System.Linq的;
使用Microsoft.Data.Entity;
使用Microsoft.Extensions.DependencyInjection;命名空间apiservice.Models
{
    公共静态类的sampleData
    {
        公共静态无效初始化(的IServiceProvider的ServiceProvider){
            VAR背景= serviceProvider.GetService&LT; BookContext&GT;();
            context.Database.Migrate();
            如果(!context.Books.Any()){
                VAR奥斯汀= context.Authors.Add(
                    新作者{名字=奥斯汀,名字=简})实体。
                VAR狄更斯= context.Authors.Add(
                    新作者{名字=狄更斯,名字=查尔斯})实体。
                VAR塞万提斯= context.Authors.Add(
                    新作者{名字=塞万提斯,名字=米盖尔})实体。                context.Books.AddRange(
                    新书 {
                        标题=傲慢与prejudice
                        年= 1813,
                        作者奥斯丁=,
                        价格= 9.99M,
                        类型=礼仪喜剧
                    },
                    新书 {
                        标题=诺桑觉寺,
                        年= 1817,
                        作者奥斯丁=,
                        价格= 12.95M,
                        类型=哥特式的蠢事
                    },
                    新书 {
                        标题=大卫·科波菲尔,
                        年= 1850,
                        作者狄更斯=,
                        价格= 15,
                        类型=成长小说
                    },
                    新书 {
                        标题=堂吉诃德
                        年= 1617,
                        作者塞万提斯=,
                        价格= 8.95M,
                        类型=流浪汉
                    }
                );                context.SaveChanges();
            }
        }
    }
}

project.json 的有以下依赖使用部分:

{
  依赖:{
    Microsoft.AspNet.Diagnostics:1.0.0-RC1决赛
    Microsoft.AspNet.IISPlatformHandler:1.0.0-RC1决赛
    Microsoft.AspNet.Mvc:6.0.0-RC1决赛
    Microsoft.AspNet.Mvc.TagHelpers:6.0.0-RC1决赛
    Microsoft.AspNet.Server.Kestrel:1.0.0-RC1决赛
    Microsoft.AspNet.StaticFiles:1.0.0-RC1决赛
    Microsoft.AspNet.Tooling.Razor:1.0.0-RC1决赛
    Microsoft.Extensions.Configuration.FileProviderExtensions:1.0.0-RC1决赛
    Microsoft.Extensions.Configuration.Json:1.0.0-RC1决赛
    Microsoft.Extensions.Logging:1.0.0-RC1决赛
    Microsoft.Extensions.Logging.Console:1.0.0-RC1决赛
    Microsoft.Extensions.Logging.Debug:1.0.0-RC1决赛
    Microsoft.Extensions.DependencyInjection:1.0.0-RC1决赛
    Microsoft.Extensions codeGenerators.Mvc:1.0.0-RC1决赛
    Microsoft.VisualStudio.Web.BrowserLink.Loader:14.0.0-RC1决赛
    EntityFramework.MicrosoftSqlServer:7.0.0-RC1决赛
    EntityFramework.MicrosoftSqlServer.Design:7.0.0-RC1决赛
    EntityFramework.Commands:7.0.0-RC1决赛
    System.Net.Http:4.0.1-β-23516,
    Microsoft.Net.Http:2.2.29,
    Newtonsoft.Json:8.0.1
  },
  命令:{
    网络:Microsoft.AspNet.Server.Kestrel
    EF:EntityFramework.Commands
  },
  构架: {
    dnx451:{},
    dnxcore50:{}
  }
}

appsettings.json 的固定

  {
  记录:{
    IncludeScopes:假的,
    LogLevel的:{
      默认值:详细,
      系统:信息,
      微软:信息
    },
    数据:{
        DefaultConnection:{
            的ConnectionString:服务器=本地主机;数据库=图书城;用户ID = SA;密码= ******; Trusted_Connection = TRUE;
        }
    }
  }
}

  {
  记录:{
    IncludeScopes:假的,
    LogLevel的:{
      默认值:详细,
      系统:信息,
      微软:信息
    }
  },
  数据:{
    DefaultConnection:{
      的ConnectionString:服务器=本地主机;数据库=图书城;用户ID = SA;密码= ******; Trusted_Connection = TRUE;
    }
  }
}

数据不能在登录

和的使用以下 Startup.cs

使用apiservice.Models;
使用Microsoft.AspNet.Builder;
使用Microsoft.AspNet.Hosting;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Logging;
使用Microsoft.Data.Entity;
命名空间apiservice
{
    公共类启动
    {
        公共静态IConfigurationRoot配置{搞定;组;}        公共启动(IHostingEnvironment ENV)
        {
            //设置配置源。
            VAR建设者=新ConfigurationBuilder()
                .AddJsonFile(appsettings.json)
                .AddJsonFile($。AppSettings的{} env.EnvironmentName以.json,可选:真)
                .AddEnvironmentVariables();
            配置= builder.Build();
        }        //此方法被运行时调用。使用此方法可以服务于容器中添加。
        公共无效ConfigureServices(IServiceCollection服务)
        {
            //添加架构服务。
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext&LT; BookContext&GT;(选项=&GT;
                    options.UseSqlServer(配置[数据:DefaultConnection:ConnectionString的])
                );            services.AddMvc();
        }        公共无效配置(IApplicationBuilder应用,ILoggerFactory的LoggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection(记录));
            loggerFactory.AddDebug();            //我们需要前执行以下两条命令            // DNU恢复
            // DNX EF迁移初始添加
            // DNX EF数据库更新            //有关部署期间创建数据库的更多详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=615859
            尝试
            {
                使用(VAR serviceScope = app.ApplicationServices.GetRequiredService&LT; IServiceScopeFactory&GT;()
                    .CreateScope())
                {
                    serviceScope.ServiceProvider.GetService&LT; BookContext&GT;()
                         .Database.Migrate();
                }
            }
            赶上{}            app.UseIISPlatformHandler();            app.UseStaticFiles();            app.UseMvc(路线=&GT;
            {
                routes.MapRoute(
                    名称:默认,
                    模板:{控制器= HOME} / {行动=指数} / {?ID});
            });            SampleData.Initialize(app.ApplicationServices);
        }        //入口点的应用程序。
        公共静态无效的主要(字串[] args)=&GT; WebApplication.Run&LT;&启动GT;(参数);
    }
}

我在评论中写道配置方法,一要执行的命令

DNX EF迁移初始添加
DNX EF数据库更新

终究包被恢复。 DNX EF迁移添加初始将与像 20160101205609_Initial的文件的项目创建附加的迁移文件夹的.cs BookContextModelSnapshot.cs 。在 appsettings.json 定义的数据库将 DNX EF数据库更新创建,这将充满来自测试数据 SampleData.cs 与程序分叉时。

当我移动的样子的package.json 包文件夹(在同一级别内​​为 project.json )。您可以使用包不大,但运动使故宫包可见光和管理在Visual Studio中。

I am having the same issue. I have added the following dependencies in my project.json file:

"dependencies": {
    "EntityFramework": "7.0.0-beta4",
    "EntityFramework.Core": "7.0.0-rc1-final",
    "EntityFramework.SqlServer": "7.0.0-beta8",
    "EntityFramework.Commands": "7.0.0-rc1-final"
  },
  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel",
    "ef": "EntityFramework.Commands"
  },
...

I used dnu install EntityFramework and dnu install EntityFramework.SqlServer to install the packages. "EntityFramework": "7.0.0-beta4", was written under dependencies node automatically by the installer itself.

Issue / Question 1: To my surprise when I was pulling in the intellisence for EntityFramework the available version I am presenting with is only 6.1.3!

Issue 2: When I am compiling my application using dnu build (I ran dnu restore command after adding EntityFramework assemblies (.Core and .Commands) manually under dependencies node I am getting a bunch of compilation errors:

The type 'DbContextOptionsBuilder' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=null'.
D:\Projects\aspnet\apiservice\Models\SampleData.cs(12,41): DNXCore,Version=v5.0 error CS0308: The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments
D:\Projects\aspnet\apiservice\Models\SampleData.cs(13,32): DNXCore,Version=v5.0 error CS0308: The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments
D:\Projects\aspnet\apiservice\Models\SampleData.cs(14,29): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'Books' and no extension method 'Books' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
D:\Projects\aspnet\apiservice\Models\SampleData.cs(15,42): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'Authors' and no extension method 'Authors' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
D:\Projects\aspnet\apiservice\Models\SampleData.cs(17,43): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'Authors' and no extension method 'Authors' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
D:\Projects\aspnet\apiservice\Models\SampleData.cs(19,45): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'Authors' and no extension method 'Authors' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
D:\Projects\aspnet\apiservice\Models\SampleData.cs(22,29): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'Books' and no extension method 'Books' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
D:\Projects\aspnet\apiservice\Models\SampleData.cs(61,29): DNXCore,Version=v5.0 error CS1061: 'object' does not contain a definition for 'SaveChanges' and no extension method 'SaveChanges' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

If I remove .Core and .Commands assembly references the project build fine.

Then I changed the version for EntityFramework.Core to 7.0.0.0 as mentioned in the error:

The type 'DbContextOptionsBuilder' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=null'.

But dnu restore now gave me the following:

Unable to locate Dependency EntityFramework.Core >= 7.0.0
Writing lock file D:\Projects\aspnet\apiservice\project.lock.json
Restore complete, 6675ms elapsed

Errors in D:\Projects\aspnet\apiservice\project.json
    Unable to locate Dependency EntityFramework.Core >= 7.0.0

I am a newbie in asp.net 5 Visual Studio code platform. And I cannot use Visual Studio 2015 as other members of the dev team are using OSX.

This is a learning project for me. Requirement is pretty straight forward. Connect a SQL Server database, pull master-child relational data using Entity Framework.

Screwed up! Help!!

Thanks in advance

UPDATE

  1. I can see the major changes in project.json for all packages with version 7.0.0-rc1-final. I used Yeoman generator tool to create the project but looks like you corrected the package through Visual Studio. So, do I have to update the package version manually and restore them?

  2. When the project was created/generated by Yeoman, there was no global.json file added by default. Can I add this manually and put the code inside by hand? Will this work?

  3. I can also see some additional package references are added to project.json file which were not added by default by Yeoman generator - like Microsoft.AspNet.Tooling.Razor, Microsoft.Extensions.Logging.Console, Microsoft.Extensions.Logging.Debug, Microsoft.Extensions.Logging etc. Are they all being used effectively in the project? Because the generator did not add them automatically, again, can I add them manually and restore using dnu restore? Will this have any impact on the project if I add them manually?

解决方案

You should remove "EntityFramework" from dependencies (remove the line "EntityFramework": "7.0.0-beta4") and use

"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final",
"EntityFramework.Commands": "7.0.0-rc1-final"

only. You don't need to add "EntityFramework.Core": "7.0.0-rc1-final" explicitly if you added "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final" because of the following dependencies:

"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final":
    "EntityFramework.Relational": "7.0.0-rc1-final":
        "EntityFramework.Core": "7.0.0-rc1-final"

I mean that EntityFramework.MicrosoftSqlServer requires EntityFramework.Relational, which requires EntityFramework.Core.

You should verify additionally that global.json contains "sdk": { "version": "1.0.0-rc1-update1" }.

UPDATED: I examined the test project which you uploaded. It had many small problems. I modified it. You can download the modified project here. Don't forget to fix the ConnectionString used in appsettings.json, it contains the Server which I used at me.

The most important changes are the usage of more simple BookContext.cs:

using Microsoft.Data.Entity;

namespace apiservice.Models {
    public class BookContext : DbContext {
        public DbSet<Author> Authors { get; set; }
        public DbSet<Book> Books { get; set; }
    }
}

the usage of the following SampleData.cs

using System;
using System.Linq;
using Microsoft.Data.Entity;
using Microsoft.Extensions.DependencyInjection;

namespace apiservice.Models
{
    public static class SampleData
    {
        public static void Initialize(IServiceProvider serviceProvider) {
            var context=serviceProvider.GetService<BookContext>();
            context.Database.Migrate();
            if (!context.Books.Any()) {
                var austen = context.Authors.Add(
                    new Author { LastName = "Austen", FirstName = "Jane" }).Entity;
                var dickens = context.Authors.Add(
                    new Author { LastName = "Dickens", FirstName = "Charles" }).Entity;
                var cervantes = context.Authors.Add(
                    new Author { LastName = "Cervantes", FirstName = "Miguel" }).Entity;

                context.Books.AddRange(
                    new Book {
                        Title = "Pride and Prejudice",
                        Year = 1813,
                        Author = austen,
                        Price = 9.99M,
                        Genre = "Comedy of manners"
                    },
                    new Book {
                        Title = "Northanger Abbey",
                        Year = 1817,
                        Author = austen,
                        Price = 12.95M,
                        Genre = "Gothic parody"
                    },
                    new Book {
                        Title = "David Copperfield",
                        Year = 1850,
                        Author = dickens,
                        Price = 15,
                        Genre = "Bildungsroman"
                    },
                    new Book {
                        Title = "Don Quixote",
                        Year = 1617,
                        Author = cervantes,
                        Price = 8.95M,
                        Genre = "Picaresque"
                    }
                );

                context.SaveChanges(); 
            }
        }
    }    
}

the usage of project.json with the following dependencies part:

{
  "dependencies": {
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
    "Microsoft.Extensions.DependencyInjection": "1.0.0-rc1-final",
    "Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final",
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "System.Net.Http": "4.0.1-beta-23516",
    "Microsoft.Net.Http": "2.2.29",
    "Newtonsoft.Json": "8.0.1"
  },
  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel",
    "ef": "EntityFramework.Commands"
  },
  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  }
}

fixing of appsettings.json from

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Verbose",
      "System": "Information",
      "Microsoft": "Information"
    },
    "Data": {
        "DefaultConnection":{
            "ConnectionString": "Server=localhost;Database=BookStore;User Id=sa; Password=******;Trusted_Connection=true;"
        }
    }
  }
}

to

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Verbose",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "Server=localhost;Database=BookStore;User Id=sa; Password=******;Trusted_Connection=true;"
    }
  }
}

(Data should not be under Logging)

and the usage of the following Startup.cs

using apiservice.Models;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Data.Entity;


namespace apiservice
{
    public class Startup
    {
        public static IConfigurationRoot Configuration {get; set;}

        public Startup(IHostingEnvironment env)
        {
            // Set up configuration sources.
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<BookContext>(options => 
                    options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])
                );

            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            // we need to execute the following two commands before

            //    dnu restore
            //    dnx ef migrations add Initial
            //    dnx ef database update   

            // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
            try
            {
                using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
                    .CreateScope())
                {
                    serviceScope.ServiceProvider.GetService<BookContext>()
                         .Database.Migrate();
                }
            }
            catch { }

            app.UseIISPlatformHandler();

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            SampleData.Initialize(app.ApplicationServices);
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}

I wrote in the comment inside of Configure method that one should execute the commands

dnx ef migrations add Initial
dnx ef database update

after all packages are restored. dnx ef migrations add Initial will create additional Migrations folder in the project with the files like 20160101205609_Initial.cs and BookContextModelSnapshot.cs. The database defined in appsettings.json will be created by dnx ef database update and it will be filled with test data from SampleData.cs during forking with the program.

By the way I moved package.json inside of the package folder (on the same level as project.json). You use the packages not, but the movement makes the npm packages visible and management in Visual Studio.

这篇关于ASP.NET 5 EntityFramework.Core 7.0.0-RC1-最后一个问题 - 编译器7.0.0.0要被引用未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆