. NET Core Self-Learning Essays (III.)--What about our entire database?

Source: Internet
Author: User
Tags microsoft sql server mssql sqlite

As mentioned in the first article, we will introduce the knowledge of database. Okay, well, we're going to learn more about database now!

Before learning about database knowledge, let's look at what the main databases are:
1, MSSQL, which is often said Microsoft SQL Server. For non-Windows systems, it is not possible to install-at least not before the Linux version, but I heard that Microsoft intended to produce for Linux, but the function is lower.

2, MYSQL/MARIDB, this is the most common database for each company. Why, free chant, do not spend silver. He who does not spend money does not like it. And his best partner is PHP. It is said that PHP 7.0 is used as a method of the PDO link database, which provides a better way for database operations.

3, Sqlite, you can think of him as access as a database of a file, but also can be understood as an access based on a comprehensive independent SQL database file

4, Oracle, Oracle's best technology product--since Java was received, the feeling of Java was well kidnapped by Oracle when the pressure village lady, and little brother MySQL became a small urge ... Uh... It's over. Oracle's strong database control capabilities are built on top of SQL, so it's called a big-god database.

There are other, he is, can be ignored. After all, use less.

Below, I use the Microsoft common MSSQL as an example to show you how it plays. Uh... My main use is MSSQL 2012, in fact, the others are almost pulled.

Well, we're still building an environment like before. I'm going to use VS2015 to get the whole. Why, because I am worried that I am happy to write wrong, this is very headache (^^)! To do this, I won't use some of the methods that only Visual Studio can use, but instead use the full platform to play.

Project name I also think well, it is called "TestProject", more COOL name, a look will know what this is doing!

By the way of the first article, we choose to put testproject that good. The good looks are:

Well, if we want to get. NET Core to communicate with the database, we need to invoke the appropriate communication package. How are these packages called? It is, of course, implemented by setting up its Project.json engineering files.

The first article said that Project.json can be used to call NuGet to set the appropriate library.

First, we open the Project.json and modify it as follows:
1, add "Microsoft.EntityFrameworkCore.SqlServer" in "dependencies", the version is "1.1.0", then add " Microsoft.EntityFrameworkCore.Design ", version is" 1.1.0 "and the type is" build ".

"Microsoft.EntityFrameworkCore.SqlServer""1.1.0","Microsoft.EntityFrameworkCore.Design": {    "version""1.1.0",    "type""build"}

With this modification, we see two more guys in the references:

If other databases, such as MySQL, Sqlite, actually, you change the "Microsoft.EntityFrameworkCore.SqlServer" to the corresponding NuGet, for example: " Microsoft.EntityFrameworkCore.Sqlite "," MySQL.Data.Entities ".

2. The class that we want to use for first Code implementation. How do you do it? The way is to create a class to implement. Let's start with a new folder called "Model", so that's what MVC looks like (><)! Then create a class in it, named "MyDataBase"-My database!

Look, don't build the wrong! After all,. NET core only recognizes. NET core, which is largely ignored by the. NET Framework.

The results should be as follows:

3, change the code in the MyDataBase.cs
We first look at, MyDataBase.cs in the original look like, so everyone unified, or the posture is wrong, straight down, probably to a wonderful.

OK, we have modified it below.

First, we add a "using Microsoft.entityframeworkcore" to a large stack of using. This gives you the right namespace to support the following code.

After that, add ": DbContext" after the public class myDataBase.

Then set up two methods in the method-in fact, the equivalent of a "table" in the database.

publicgetset; }publicgetset; }

Among them, the Users and News must be consistent with the database, and MyUser and Mynews is the class composition-its structure, to see the mood. ^^

With two tables, we have to think about it. No, we don't even have a database, there are two of them, so we're going to connect to the database (honestly, I feel like I'm putting the cart before the horse). The code is:

protectedoverridevoidOnConfiguring(DbContextOptionsBuilder optionsBuilder){    optionsBuilder.UseSqlServer(@"Server=SQLOLEDB;Data Source=(local);uid=sa;pwd=za;DataBase=myDataBase");}

Where we see a usesqlserver method in the Optionsbuilder object, indicating that this is a connection to SQL Server, if you select the corresponding class library in Project.json, then the Usesqlserver will also become other database connection methods, such as Sqlite is Usesqlite.

The following @ "Server=sqloledb;data source= (local); Uid=sa;pwd=za;database=mydatabase" is the connection string for the database and is modified to suit your needs

Below, let's look at the MyDataBase.cs file that looks like this:

We found that in this file, the IDE made two error points, and maybe you won't find it in other editors-"MyUser" and "Mynews".

It says that these two goods are the class of the table structure in the database, so we'd better set up the corresponding table in the database first. Don't forget the database name is: mydata--See the connection string.

I use SQL Server 2012 as follows:

Users table (primary key: ID, self-increment):

News table (primary key: news_id, self-increment):

Well, with the watch, we're going to implement both tables. How to achieve, of course, to the two guys to establish the corresponding class.

According to Custom, my personal habits, you are casual. is to build a dbo in the model to hold the classes corresponding to the corresponding table. After that, we create the corresponding two class files in the dbo folder: MyUser.cs, MyNews.cs

OK, let's make it concrete, first the whole MyUser.cs, the code is as follows:

using System;using System.ComponentModel.DataAnnotations;namespace testProject.Model.dbo{    publicclass MyUser    {        [Key]        publicintgetset; }        publicstringgetset; }        publicgetset; }    }}

As we can see, the method ID, name, and Createtime correspond to each field in the database table Users, and the even type is one by one.

The [key] label labels the ID as a keyword. If you want to invoke [Key] in a class, you must introduce the command space: System.ComponentModel.DataAnnotations.

Thus the class of the first table is implemented, and the second MyNews.cs as above, we take "Class drawing class".

using  System; using  System.componentmodel.dataannotations;namespace testproject.model.dbo{public  Span class= "Hljs-keyword" >class mynews {[Key] public  int  news_id {get ; set ; } public  string  news_name {get ; set ; } public  string  news_content {get ; set ; } public  DateTime? news_createtime {get ; set ; } }}

After the two classes have been implemented, we are going to introduce these two guys into the database class MyDataBase.cs. Because it's all in the dbo folder, we just have to join the namespace in MyDataBase.cs: TestProject.Model.dbo.

In this way, the database connection is made. In fact, as long as the call to the Model is the same as the usual MVC.

What the??? Someone will not operate ... God, that ... OK, let me introduce you briefly! t_t

We first set up a method in the Controller folder HomeController.cs, called Writeuser.

public string Writeuser () {Model. MyDataBasedb = new Model. MyDataBase();Model. dbo. MyUseruser = new Model. dbo. MyUser{name ="Mysteriousman", Createtime = DateTime. now};Db. Users. ADD(user);Db. SaveChanges();Return"OK";}

To run the results Web page:

Run the results database:

In this way, the basic operation of the. NET Core database is such a bird ~ ~ ~

. NET Core Self-Learning Essays (III.)--What about our entire database?

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.