C# .NET EF framework webapi installation using sqlite

Sqlite installation and use

Sqlite features

Sqlite is a lightweight database that does not require installation and can be used across platforms. It is the optimal solution for small software, small projects, and rapid transplantation. There is no need to install the database first like mysql and Sqlserver, and then use it.

Environment installation

Net.Core =6.0

project files

insert image description here

  • EF.Sqlite: Sqlite lightweight database, no need to install, only need to import Nuget package

insert image description here

  • EF.Tool: Database transfer tool, .Net defaults to Sqlserver. This is to transfer database management objects from Sqlserver to sqlite

insert image description here

  • EF framework introduction

insert image description here

What are EFs?

EF is an ORM entity-relationship database, what is ORM? Simply put, ORM means that the objects that our program runs are consistent with the objects stored in the database. The database is just one place where the data we run is persisted. A database is simply a large, persistent array of objects.

example

程序 数据库 新建实体对象 实体对象进行修改 存储持久化,Id自增 查询数据库,返回符合的对象数组 程序 数据库

EF uses

Create two new files

  • DbContext.cs: database corresponding class, used for sql operation
  • DtName.cs: The corresponding class of the data table, used to design the corresponding fields of the data table, Id does not need

example

Database: MyToDoDbContext, inherited from DbContext

using Microsoft.EntityFrameworkCore;

namespace MyToDo.Api.Context
{
    
    
    public class MyToDoContext:DbContext
    {
    
    
        public MyToDoContext(DbContextOptions<MyToDoContext> options):base(options)
        {
    
    //继承DbContext建库方法
            
        }
        public DbSet<ToDo> ToDo {
    
     get; set; }


    }
}



Data table corresponding entity: ToDo class

namespace MyToDo.Api.Context
{
    
    
    public class ToDo 
    {
    
    
        public string Title {
    
     get; set; }

        public string Content {
    
     get; set; }

        public int Status {
    
     get; set; }
    }
}

Add database connection string in json

insert image description here

insert image description here

{
    
    
  "ConnectionStrings": {
    
    
    "ToDoConnection": "Data Source=to.do"
  },
  ...
}

Set in Program, refer to sqlite

insert image description here

insert image description here

builder.Services.AddDbContext<MyToDoContext>(options =>
{
    
    
//这个是我们在appsettings.json里面添加的数据库连接串
    var connectionStr = builder.Configuration.GetConnectionString("ToDoConnection");

    options.UseSqlite(connectionStr);
});

Nuget console

insert image description here

insert image description here

insert image description here
Nuget management tool default project selection WebApi project

insert image description here

We enter the code in the console: Add-Migration MyToDo (your manager name)

insert image description here
Then enter Update-Database

insert image description here

If you report this error, remember to check if your configuration name is correct

insert image description here

insert image description here

insert image description here
We have created a new database here, that is, the creation is successful

insert image description here

Guess you like

Origin blog.csdn.net/qq_44695769/article/details/131116806