Entity Framework (EF) Core is a lightweight, extensible, open-source, and cross-platform version of the popular Entity Framework data access technology.
EF Core can be used as an object-relational mapper (O/RM) so that .NET developers can work with databases with .NET objects so that they don't have to write most of the data access code frequently.
Database Provider:The hyperlink login is visible.
The environment used in this article:
- Open Tools: VS 2017
- dotnet version: 2.1.301
- Database: SQL Sever 2012
Create a project
1: BaiDuPan.WebUI is a ASP.NET to create a Core web application;
2: BaiDuPan.DomainModel is to create a class library (.NET Core);
Add an Entity Framework Core reference
Developed with vs2017. NET Framework project, when adding ef 6, you can directly add it by right-clicking on the project, as shown in the figure below:
However, EF cores cannot be added in the above way.
PM command:
By the above order,The program automatically adds the required dependency packagesSo, there's no need to add the Microsoft.EntityFrameworkCore package.
Add context and entity models
Movie Models:
PanDb database context:
Startup class, which adds database context dependency injection (Here you need to add a DomainModel reference to the WebUI):
The above code requires the project to reference Microsoft.EntityFrameworkCore.SqlServer, but BaiDuPan.WebUI does not need to reference Microsoft.EntityFrameworkCore.SqlServer, why is it not reported an error?
Microsoft.AspNetCore.App package includes all the features of ASP.NET Core 2.1 and later and Entity Framework Core 2.1 and later. Default project templates for ASP.NET Core 2.1 and later use this package. Microsoft.AspNetCore.App packages are recommended for applications with ASP.NET Core 2.1 and later and Entity Framework Core 2.1 and later.
appsettings.json Configure the database connection string:
Try to start the project with the following error:
Error NU1107 detects a version conflict in Microsoft.EntityFrameworkCore. Referencing the package directly from the project solves this problem. BaiDuPan.WebUI -> BaiDuPan.DomainModel -> Microsoft.EntityFrameworkCore.SqlServer 2.2.4 -> Microsoft.EntityFrameworkCore.Relational 2.2.4 -> Microsoft.EntityFrameworkCore (>= 2.2.4) BaiDuPan.WebUI -> Microsoft.AspNetCore.App 2.1.1 -> Microsoft.EntityFrameworkCore (>= 2.1.1 && < 2.2.0). BaiDuPan.WebUI C:\Users\itsvse_pc\Source\Repos\baidupan\BaiDuPan.WebUI\BaiDuPan.WebUI.csproj 1 The reason is that there is a version conflict between the two projects, try to install a higher version of ef core in BaiDuPan.WebUI, the new error is as follows:
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.4 GEThttps://api.nuget.org/v3/registr ... qlserver/index.json CACHEhttps://api.nuget.org/v3/registr ... qlserver/index.json Reverting the package of C:\Users\itsvse_pc\Source\Repos\baidupan\BaiDuPan.WebUI\BaiDuPan.WebUI.csproj...
NU1608: 检测到的包版本在依赖项约束之外: Microsoft.AspNetCore.App 2.1.1 需要 Microsoft.EntityFrameworkCore.SqlServer (>= 2.1.1 && < 2.2.0),但版本 Microsoft.EntityFrameworkCore.SqlServer 2.2.4 已解决。 Install-Package: NU1107: Version conflict detected in Microsoft.EntityFrameworkCore. Referencing the package directly from the project solves this problem. BaiDuPan.WebUI -> Microsoft.EntityFrameworkCore.SqlServer 2.2.4 -> Microsoft.EntityFrameworkCore.Relational 2.2.4 -> Microsoft.EntityFrameworkCore (>= 2.2.4) BaiDuPan.WebUI -> Microsoft.AspNetCore.App 2.1.1 -> Microsoft.EntityFrameworkCore (>= 2.1.1 && < 2.2.0). Location Line: 1 Characters: 1 + Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2. ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Install-Package], Exception + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
Install-Package: Package restore failed. Rolling back package changes for "BaiDuPan.WebUI". Location Line: 1 Characters: 1 + Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2. ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Install-Package], Exception + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
Time Apred: 00:00:01.4208784 PM>
What to do? The solution is as follows:
Downgrade, the BaiDuPan.WebUI project references Microsoft.EntityFrameworkCore 2.1.1, so we re-execute the nuget command in the BaiDuPan.DomainModel project, as follows:
This way, we guarantee that all projects have the same EF Core version.
Initialize the database
The database and table are successfully created as shown in the following image:
Add the MovieController controller
The code is as follows:
The Index method is to query all the data in the database Movie table, and the Create method is to add new data, let's try to add a piece of data and request it with postman, as shown in the figure below:
The hyperlink login is visible.POST request
The program was able to execute smoothly, no exceptions were thrown, proving that the insertion was successful, and the access was attempted:The hyperlink login is visible.Query all data as shown in the figure below:
As for modifications and deletions, it goes without saying!
Resources:
Entity Framework Core:The hyperlink login is visible.
ASP.NET Core:The hyperlink login is visible.
(End)
|