c# – ASP.NET 5 EntityFramework.Core 7.0.0-rc1-final问题 – 编译器想要7.0.0.0被引用,没有找到

c# – ASP.NET 5 EntityFramework.Core 7.0.0-rc1-final问题 – 编译器想要7.0.0.0被引用,没有找到,第1张

概述我有同样的问题.我在project.json文件中添加了以下依赖项: "dependencies": { "EntityFramework": "7.0.0-beta4", "EntityFramework.Core": "7.0.0-rc1-final", "EntityFramework.SqlServer": "7.0.0-beta8", "EntityFra 我有同样的问题.我在project.Json文件中添加了以下依赖项:
"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安装EntityFramework和dnu安装EntityFramework.sqlServer来安装软件包. “EntityFramework”:“7.0.0-beta4”,由安装程序本身自动编写在依赖节点下.

问题1:令人惊讶的是,当我提到EntityFramework的智能化时,我提供的可用版本只有6.1.3!

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

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 argumentsD:\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 argumentsD:\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,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,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程序集引用项目构建正常.

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

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

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

Unable to locate Dependency EntityFramework.Core >= 7.0.0Writing lock file D:\Projects\aspnet\APIservice\project.lock.JsonRestore complete,6675ms elapsedErrors in D:\Projects\aspnet\APIservice\project.Json    Unable to locate Dependency EntityFramework.Core >= 7.0.0

我是一个新手在asp.net 5 Visual Studio代码平台.我不能使用Visual Studio 2015作为开发团队的其他成员使用OSX.

这是一个为我学习的项目.要求很简单.连接Sql Server数据库,使用实体框架来拉大师级关系数据.

搞砸了!帮帮我!!

提前致谢

UPDATE

>我可以看到project.Json对于版本7.0.0-rc1-final的所有软件包的重大变化.我使用Yeoman生成器工具来创建项目,但是看起来像是通过Visual Studio更正了包.那么,我必须手动更新软件包版本并恢复它们吗?
>当项目由Yeoman创建/生成时,默认情况下没有添加global.Json文件.我可以手动添加这个代码吗?这会工作吗
>我还可以看到一些额外的包引用被添加到project.Json文件,默认情况下没有被Yeoman生成器添加 – 如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-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”:“7.0.0-rc1-final”

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

您应该另外验证global.Json包含“sdk”:{“version”:“1.0.0-rc1-update1”}.

UPDATED:我检查了你上传的测试项目.它有很多小问题.我修改了您可以下载修改的项目here.不要忘记修复在appsettings.Json中使用的ConnectionString,它包含我在我使用的服务器.

最重要的更改是使用更简单的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,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": {    "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.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"  },"frameworks": {    "dnx451": { },"dnxcore50": { }  }}

固定appsettings.Json从

{  "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,"Microsoft": "information"    }  },"Data": {    "DefaultConnection": {      "ConnectionString": "Server=localhost;Database=BookStore;User ID=sa; Password=******;Trusted_Connection=true;"    }  }}

(数据不应该在日志记录下)

并使用以下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);    }}

我在配置方法中的注释中写道,应该执行命令

dnx ef migrations add Initialdnx ef database update

所有包恢复后. dnx ef migrations add Initial将在项目中创建其他Migrations文件夹,其中包含像20160101205609_Initial.cs和BookContextModelSnapshot.cs这样的文件.在appsettings.Json中定义的数据库将由dnx ef数据库更新创建,并且在使用该程序进行分组时将从SampleData.cs填充测试数据.

顺便说一句,我将package.Json移动到包文件夹(与project.Json相同的级别)内.您使用的包不是,但运动使得npm包可见和管理在Visual Studio中.

总结

以上是内存溢出为你收集整理的c# – ASP.NET 5 EntityFramework.Core 7.0.0-rc1-final问题 – 编译器想要7.0.0.0被引用,没有找到全部内容,希望文章能够帮你解决c# – ASP.NET 5 EntityFramework.Core 7.0.0-rc1-final问题 – 编译器想要7.0.0.0被引用,没有找到所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1259379.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存