简体   繁体   English

将 EF(不是 EF 核心)应用到 ASP.NET 核心项目

[英]Apply EF (not EF core) to an ASP.NET Core project

Goal:目标:

Use Entity Framework (not EF Core) in an ASP.NET Core project.在 ASP.NET 核心项目中使用实体框架(不是 EF 核心)。

According to instruction you have to create a .net project and then you go to ASP.NET Core project and add it to references by selecting this EF project.根据说明,您必须创建一个 .net 项目,然后将 go 到 ASP.NET 核心项目,并通过选择此 EF 项目将其添加到引用中。 ( https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-3.0#reference-full-framework-and-ef6-in-the-asp-net-core-project ) ( https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-3.0#reference-full-framework-and-ef6-in-the-asp-net -核心项目

Problem: I get an error message问题:我收到一条错误消息

Error CS0012 The type 'DbSet<>' is defined in an assembly that is not referenced.错误 CS0012 类型“DbSet<>”在未引用的程序集中定义。 You must add a reference to assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'您必须添加对程序集 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 的引用

I have already applied EF in the project but I still get this error.我已经在项目中应用了 EF,但仍然出现此错误。

I have also applied a github link that you can download the solution and its project我还应用了 github 链接,您可以下载解决方案及其项目
https://github.com/candyboyyy/ef https://github.com/candyboyyy/ef

Info:信息:
https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-3.0#reference-full-framework-and-ef6-in-the-asp-net-core-project https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-3.0#reference-full-framework-and-ef6-in-the-asp-net-核心项目

CREATE TABLE [dbo].[Blogs]
(
    [BlogId] [INT] IDENTITY(1,1) NOT NULL,
    [Name] [NVARCHAR](200) NULL,
    [Url] [NVARCHAR](200) NULL,

    CONSTRAINT [PK_dbo.Blogs] 
        PRIMARY KEY CLUSTERED ([BlogId] ASC)
                    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                          IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                          ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ASP.NET Core != .NET Core ASP.NET 核心!= .NET 核心

ASP.NET Core can run on .NET Core or .NET Framework (See Introduction to ASP.NET Core ). ASP.NET 内核可以在 .NET 内核或 .NET 框架上运行(参见 Z9E0DA8438E1E38A1C30F4B76CE73B8 框架简介)。

EF Compatibility英孚兼容性

EF >= 6.3 EF >= 6.3

From 6.3 EF supports .NET Core.从 6.3 开始,EF 支持 .NET 内核。

Announcing Entity Framework 6.3 Preview with .NET Core Support 宣布具有 .NET 核心支持的 Entity Framework 6.3 预览版

In fact, the main goal of this release is to facilitate migrating existing applications that use EF 6 to .NET Core 3.0.实际上,此版本的主要目标是促进将使用 EF 6 的现有应用程序迁移到 .NET Core 3.0。

EF <6.3 EF <6.3

If you want to use EF<6.3 from ASP.Core you need to target .NET framework.如果你想使用 ASP.Core 中的 EF<6.3,你需要以 .NET 框架为目标。 The following is from Get Started with ASP.NET Core and Entity Framework 6 .以下内容来自ASP.NET 核心和实体框架 6 入门

To use Entity Framework 6, your project has to compile against .NET Framework, as Entity Framework 6 doesn't support .NET Core.要使用实体框架 6,您的项目必须针对 .NET 框架进行编译,因为实体框架 6 不支持 .NET 核心。

Your solution specifically您的具体解决方案

The library project in your solution needs to be converted to a .NET Core project.您的解决方案中的库项目需要转换为 .NET 核心项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM