ASP.NET 5 EntityFramework.Core 7.0.0-rc1-final 问题 - 编译器希望引用 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

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

问题描述

我遇到了同样的问题.我在 project.json 文件中添加了以下依赖项:

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"
  },
...

我使用 dnu install EntityFrameworkdnu install EntityFramework.SqlServer 来安装包."EntityFramework": "7.0.0-beta4", 由安装程序自己自动编写在 dependencies 节点下.

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.

问题/问题 1:令我惊讶的是,当我为 EntityFramework 引入智能时,我提供的可用版本仅为 6.1.3

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!

问题 2:当我使用 dnu build 编译我的应用程序时(我在添加 EntityFramework 程序集后运行了 dnu restore 命令(.Core.Commands) 在 dependencies 节点下手动我收到一堆编译错误:

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:ProjectsaspnetapiserviceModelsSampleData.cs(12,41): DNXCore,Version=v5.0 error CS0308: The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments
D:ProjectsaspnetapiserviceModelsSampleData.cs(13,32): DNXCore,Version=v5.0 error CS0308: The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments
D:ProjectsaspnetapiserviceModelsSampleData.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:ProjectsaspnetapiserviceModelsSampleData.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:ProjectsaspnetapiserviceModelsSampleData.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:ProjectsaspnetapiserviceModelsSampleData.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:ProjectsaspnetapiserviceModelsSampleData.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:ProjectsaspnetapiserviceModelsSampleData.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?)

如果我删除 .Core.Commands 程序集引用项目构建正常.

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

然后我将 EntityFramework.Core 的版本更改为 7.0.0.0,如错误中所述:

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'.

但是 dnu restore 现在给了我以下内容:

But dnu restore now gave me the following:

Unable to locate Dependency EntityFramework.Core >= 7.0.0
Writing lock file D:Projectsaspnetapiserviceproject.lock.json
Restore complete, 6675ms elapsed

Errors in D:Projectsaspnetapiserviceproject.json
    Unable to locate Dependency EntityFramework.Core >= 7.0.0

我是 asp.net 5 Visual Studio code 平台的新手.而且我无法使用 Visual Studio 2015,因为开发团队的其他成员正在使用 OSX.

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.

这对我来说是一个学习项目.要求非常简单.连接 SQL Server 数据库,使用实体框架拉取 master-child 关系数据.

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.

搞砸了!帮助!!

提前致谢

更新

  1. 我可以在 project.json 中看到所有版本为 7.0.0-rc1-final 的包的主要变化.我使用 Yeoman 生成器工具来创建项目,但看起来您通过 Visual Studio 更正了包.那么,我是否必须手动更新软件包版本并恢复它们?

  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?

Yeoman 创建/生成项目时,默认没有添加 global.json 文件.我可以手动添加它并手动将代码放入其中吗?这行得通吗?

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?

我还可以看到一些额外的包引用被添加到 project.json 文件中,这些引用不是由 Yeoman 生成器默认添加的 - 比如 Microsoft.AspNet.Tooling.RazorMicrosoft.Extensions.Logging.ConsoleMicrosoft.Extensions.Logging.DebugMicrosoft.Extensions.Logging 等等.所有这些都在项目中得到有效利用吗?由于生成器没有自动添加它们,我是否可以手动添加它们并使用 dnu restore 恢复?如果我手动添加它们,这对项目有什么影响吗?

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" 从依赖项(删除行 "EntityFramework":"7.0.0-beta4") 并使用

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"

只有.如果您添加了 "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final ,则不需要显式添加 "EntityFramework.Core": "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"

我的意思是 EntityFramework.MicrosoftSqlServer 需要 EntityFramework.Relational,它需要 EntityFramework.Core.

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

您应该另外验证 global.json 是否包含 "sdk": { "version": "1.0.0-rc1-update1" }.

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

更新:我检查了您上传的测试项目.它有很多小问题.我修改了它.您可以在此处下载修改后的项目.不要忘记修复appsettings.json中使用的ConnectionString,它包含我在我使用的Server.强>

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.

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

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; }
    }
}

下面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(); 
            }
        }
    }    
}

project.json 与以下 dependencies 部分的用法:

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": { }
  }
}

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

{
  "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 应该不在Logging下)

(Data should not be under Logging)

以及下面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);
    }
}

我在Configure方法里面的注释中写到应该执行命令

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

dnx ef migrations add Initial
dnx ef database update

在所有包恢复后.dnx ef migrations add Initial 将在项目中创建额外的 Migrations 文件夹,其中包含 20160101205609_Initial.csBookContextModelSnapshot.cs 等文件代码>.appsettings.json 中定义的数据库将由 dnx ef database update 创建,并且在 fork 过程中会填充来自 SampleData.cs 的测试数据与程序.

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.

顺便说一下,我将 package.json 移动到包文件夹内(与 project.json 处于同一级别).您不使用包,但移动使 npm 包在 Visual Studio 中可见和管理.

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-final 问题 - 编译器希望引用 7.0.0.0,但未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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