. NET core generates entity classes based on a database

Source: Internet
Author: User
Microsoft has been on the cross-platform over the last few years, with many. NET programmers gearing up for Microsoft. Just recently, Microsoft has launched the ASP. NET core2.0 Preview.

Through a simple attempt at. NET core, I found that in the past when we developed an MVC project, we created and updated the entity model by creating a new. edmx file, but in the core, Microsoft removed the. edmx, so let's take a look at how the model class is generated in core.

Environment: vs2017 + sqlserver2012

The first step is to create a test library

CREATE DATABASE [Blogging]; Gouse [Blogging]; Gocreate TABLE [Blog] (    [BlogId] int not null IDENTITY,    [Url] nvarchar (max) is not NULL,    CONSTRAINT [Pk_blog] PRI MARY 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

Step two Create a. NET Core project

Slightly

Step three Install EF

NuGet Package Manager

Install with NuGet:

Fourth Step creating the entity model from the database

Tools–nuget Package manager–  A model the  existing.  You receive an error stating the term    recognized  the name  a cmdlet,

The project generates a model folder with the entity classes and contexts we need BloggingContext.cs

Complete! Because we only describe how to generate entity classes, so far, if you want to manipulate the entity class additions and deletions, we also need to register the context in the Startup.cs file, specifically, you can refer to Microsoft's documentation:

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.