Home  >  Article  >  Backend Development  >  .net core generates entity classes based on database

.net core generates entity classes based on database

大家讲道理
大家讲道理Original
2018-05-15 15:29:194764browse

Microsoft has been making continuous efforts in cross-platform in recent years, and many .net programmers are gearing up and have high hopes for Microsoft. Just recently, Microsoft also launched a preview version of asp .net core2.0.

Through a simple experiment with .net core, I found that when we developed MVC projects in the past, we generated and updated entities## by creating a new .edmx file. #Model, but in core, Microsoft has removed .edmx, so let me talk about how to generate the model class in core.

Environment: vs2017 + sqlserver2012

The first step is to create a test library first. The second step is to create a .net core project                                                                                                                       

CREATE DATABASE [Blogging];GOUSE [Blogging];GOCREATE TABLE [Blog] (    [BlogId] int NOT NULL IDENTITY,    [Url] nvarchar(max) NOT NULL,    CONSTRAINT [PK_Blog] PRIMARY KEY ([BlogId])
);GOCREATE TABLE [Post] (    [PostId] int NOT NULL IDENTITY,    [BlogId] int NOT NULL,    [Content] nvarchar(max),    [Title] nvarchar(max),    CONSTRAINT [PK_Post] PRIMARY KEY ([PostId]),    CONSTRAINT [FK_Post_Blog_BlogId] FOREIGN KEY ([BlogId]) REFERENCES [Blog] ([BlogId]) ON DELETE CASCADE);GOINSERT INTO [Blog] (Url) VALUES('http://blogs.msdn.com/dotnet'),
('http://blogs.msdn.com/webdev'),
('http://blogs.msdn.com/visualstudio')GO

Installation through nuget:

Step 4: Create entity model through database                  

Project Meeting Generate a model folder, which contains the entity classes and context we need BloggingContext.cs

Done! Because we only introduce how to generate entity classes, we will stop here. If we want to add, delete, modify, and query entity classes, we also need to register the context in the Startup.cs file. For details, please refer to Microsoft's documentation:

https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

The above is the detailed content of .net core generates entity classes based on database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn