"Selfless sharing: ASP. NET CORE Project Combat (fifth)" Code first create database and data tables

Source: Internet
Author: User
Tags dotnet

Catalog Index 

"Selfless sharing: ASP. NET CORE Project Combat" Catalog Index

Introduction

In this chapter we introduce the following ASP. NET Core using Codefirst to create databases and tables

to modify the EF context object, add a test class

  I changed the name and Domains changed to WKMVC. Data

We create a new folder Models for storing model classes

We'll create a new folder under models. Sysmodelsmange for zone model classes

We create a new test class Sys_user

Namespace Wkmvc. Data.models
{
public class Sys_user
{
public int ID {get; set;}
public string USERNAME {get; set;}
}
}

  

Let's change the context Applicationdbcontext (yellow highlight)

Using Microsoft.entityframeworkcore;
Using WKMVC. Data.models;

Namespace Wkmvc. Data
{
public class Applicationdbcontext:dbcontext
{
Public Applicationdbcontext (dbcontextoptions<applicationdbcontext> options)
: Base (Options)
{
}

      Public dbset<sys_user> Sys_user {get; set;}

protected override void Onmodelcreating (ModelBuilder builder)
{

Base. Onmodelcreating (builder);
}
}
}

Add a dependency package and execute add-migration from the console

Then open Wkmvc.  the following Project.json of the Data class library add Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Tools

1 {2   "version":"1.0.0-*",3 4   "Dependencies": {5     "Microsoft.entityframeworkcore":"1.0.0",6     "Microsoft.EntityFrameworkCore.SqlServer":"1.0.0",7     "netstandard.library":"1.6.0",8     "System.ComponentModel.Annotations":"4.1.0"9   },Ten  One   "Frameworks": { A     "netstandard1.6": { -       "Imports":"Dnxcore50" -     } the   }, -   "Tools": { -     "Microsoft.EntityFrameworkCore.Tools":"1.0.0-preview2-final" -   } +}

We open tools, NuGet Package Manager, package management console, select Default Project Src\wkmvc. Data

Input command:add-migration migrations

No recognition command? Looking through Microsoft's official documents, mostly using dotnet EF (described later) command, find an article

Execute this statement on the console install-package Microsoft.EntityFrameworkCore.Tools -pre , error, restore failed,. NET Core CLI does not support running commands on class libraries as of Preview 2. Despite being able to install EF tools, executing commands could throw this error message. That is, the class library is not supported, and here Microsoft gives two solutions:

First, reference https://github.com/dotnet/cli/issues/2645.

Second, modify the class library project as "app" project, and use the context factory idbcontextfactory<tcontext>, Microsoft Document address:https://docs.efproject.net/en/ Latest/miscellaneous/cli/dotnet.html?highlight=migrations#common-errors

I'm referring to the second kind of experiment. , but when I add a new class library, then reference WKMVC. Data is not available, the class library cannot reference the app project, but our context   Applicationdbcontext under this, what do you do?

Helpless, I try to add  microsoft.entityframeworkcore.tools Try to change the console default project to SRC\WKMVC execution    install-package  microsoft.entityframeworkcore.tools   -pre , and then switches back to Src\ Wkmvc. data 

It worked! Somehow, I don't know what the principle is, it's a success anyway.

Then execute update-database-verbose

This is not the right to create a database, we modify the user as an administrator, re-execute

Success, Done!

by dotnet EF

How to do this in the class library, or refer to the above Microsoft two solutions, I did not solve the problem is not to execute the command, but my context is in WKMVC. Data inside, always can not find this context, if there is a big God pointing, I will revise this article, if not first put, have the opportunity to solve, and then update. Let's introduce today, just like Microsoft's Demo, context and migrations are all used under SRC\WKMVC (web):

We open the folder where we want to add migrations (SRC\WKMVC), select this folder , Shift right-click, choose here to Open Command Window (W)

input dotnet EF--help

  

here, the documentation is clear and we need to take a few steps:

① adding Microsoft.EntityFrameworkCore.Tools


② adding Microsoft.EntityFrameworkCore.Design

then execute dotnet EF--help again

The instructions are successful, we add migrations

Input: dotnet EF migrations Add Migrations

Add complete, we can perform other operations such as dotnet EF Database Update , detailed reference https://docs.efproject.net/en/latest/miscellaneous/cli/ Dotnet.html?highlight=migrations#usage

I want to learn from you. ASP.

Just beginning to contact, the level is limited, a lot of things are their own understanding and flipping through the online god of information, if there are wrong places and do not understand the place, I hope you correct!

Although ASP. NET Core is now very hot, but many of the online information is the copy of the previous article, so there are a lot of problems I have not resolved, I hope we can help together!

Original article reproduced please respect the fruits of labor http://yuangang.cnblogs.com

"Selfless sharing: ASP. NET CORE Project Combat (fifth)" Code first create database and data tables

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.