ASP.NET Core MVC database from entry to proficiency
With the development of technology, ASP.NET Core MVC has been launched for a long time. After continuous version updates and iterations, it has become more and more perfect. This series of articles mainly explains the process involved in developing B/S systems with ASP.NET Core MVC. The relevant content is suitable for beginners, school graduates, or other people who want to engage in ASP.NET Core MVC system development. After the explanations in the previous articles, I have a preliminary understanding of ASP.NET Core MVC project creation, startup and naming conventions, creating controllers, views, models, receiving parameters, passing data, routing, page layout, wwwroot and client libraries. Razor syntax and other content, today we will continue to explain the EnityFrameworkCore and database related content in ASP.NET Core MVC, for learning and sharing purposes only.
With the development of technology, ASP.NET Core MVC has been launched for a long time. After continuous version updates and iterations, it has become more and more perfect. This series of articles mainly explains the process of developing B/S system with ASP.NET Core MVC. The relevant content involved is suitable for beginners, school graduates, or other people who want to engage in ASP.NET Core MVC system development. After the explanations in the previous articles, I have a preliminary understanding of ASP.NET Core MVC project creation, startup and naming conventions, creating controllers, views, models, receiving parameters, passing data, routing, page layout, wwwroot and client libraries. Razor syntax and other content, today we will continue to explain EnityFrameworkCore and Database and other related content are for learning and sharing only.
Introduction to EntityFrameworkCore
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), which does two things:
- Enables .NET developers to work with databases using .NET objects.
- No need to write most of the data access code as usual.
Create database
In the ASP.NET Core MVC project, there are usually two modes for environment construction: Code First and DB First. In this example, the DB First mode is mainly used, and the database used is SQL Server 2012. First create the database and name it MovieDB, and then create the data table Movie, as shown below:
Create a database by right-clicking on the database. The statement to create a table is as follows:
1 USE [MovieDB] 2 GO 3 4 /****** Object: Table [dbo].[Movie] Script Date: 2023/4/25 23:53:21 **** **/ 5 SET ANSI_NULLS ON 6 GO 7 8 SET QUOTED_IDENTIFIER ON 9 GO 10 11 SET ANSI_PADDING ON 12 GO 13 14 CREATE TABLE [dbo].[Movie]( 15 [ span>Id] [bigint] IDENTITY(1,1) NOT NULL, 16 [ span>Name] [varchar](200) NULL, 17 [ span>ReleaseDate] [datetime] NULL, 18 [ span>LeadingRole] [varchar](100) NULL, 19 [ span>Genre] [varchar](100) NULL, 20 [ span>Price] [money] NULL, 21 [ span>CreateTime] [datetime] NULL, 22 [ span>CreateUser] [varchar](50) NULL, 23 [ span>LastEditTime] [datetime] NULL, 24 [ span>LastEditUser] [varchar](50) NULL, 25 CONSTRAINT [PK_Movie ] PRIMARY KEY CLUSTERED 26 ( 27 [ span>Id] ASC 28 )WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 29 ) ON [PRIMARY] 30 31 GO 32 33 SET ANSI_PADDING OFF 34 GO
Construct test data
Before testing the application, first insert 2 pieces of test data into the data table to see the effect, as shown below:
Create data entity
After the database and data table are successfully created, you need to create the entity class MovieEntity corresponding to the data table in the ASP.NET Core MVC project, as shown below:
1 namespace DemoCoreMVC.Entities 2 { 3 ///4 /// movie entity 5 /// 6 public class MovieEntity 7 { 8 /// 9 /// Primary key unique identifier 10 thead> 19 @for (var i = 0; i < movies .Count; i++) 20 { 21 var movie = movies[i]; 22 <tr> 23 <td>@movie.Id </td> 24 <td>@movie.Name </td> 25 <td>@movie.Genre </td> 26 <td>@movie.LeadingRole </td> 27 <td>@movie.ReleaseDate </td> 28 <td>@movie.Price </td> 29 <td><a href="/Movie/Edit/@movie.Id"> Edita > | <a href="/Movie/Delete/@movie.Id">Delete</a></td> 30 </tr> 31 } 32 </table>
Run test
After the above steps, the program has been built. Run the program and enter the URL: https://localhost:7152/Movie. The opening effect is as follows:
Reference article
The main reference articles for this article are as follows:
Database troubleshooting: https://learn.microsoft.com/zh-cn/troubleshoot/sql/welcome-sql-server
Database for getting started with ASP.NET Core MVC: https://learn.microsoft.com/zh-cn/aspnet/core/tutorials/first-mvc-app/working-with-sql?view=aspnetcore -7.0&tabs=visual-studio
The above is the entire content of ASP.NET Core MVC database from entry to proficiency. It is intended to inspire others, learn together, and make progress together.
Author: Xiaoliu Gongzi
Source: http://www.cnblogs.com/hsiang/
The copyright of this article belongs to the author and the blog park. It is not easy to write an article. Originality is supported. Reprinting is welcome [like it]. Please keep this statement when reprinting, and provide a link to the original text in an obvious position on the article page. Thank you.
Follow personal public accounts and regularly update technology and workplace articles
tps://img2023.cnblogs.com/blog/1068941/202304/1068941-20230426113033889-437893885.png” alt=”” loading=”lazy”>
Reference article
The main reference articles for this article are as follows:
Database troubleshooting: https://learn.microsoft.com/zh-cn/troubleshoot/sql/welcome-sql-server
Database for getting started with ASP.NET Core MVC: https://learn.microsoft.com/zh-cn/aspnet/core/tutorials/first-mvc-app/working-with-sql?view=aspnetcore -7.0&tabs=visual-studio
The above is the entire content of ASP.NET Core MVC database from entry to proficiency. It is intended to inspire others, learn together, and make progress together.
Author: Xiaoliu Gongzi
Source: http://www.cnblogs.com/hsiang/
The copyright of this article belongs to the author and the blog park. It is not easy to write an article. Originality is supported. Reprinting is welcome [like it]. Please keep this statement when reprinting, and provide a link to the original text in an obvious position on the article page. Thank you.
Follow personal public accounts and regularly update technology and workplace articles