当前位置:网站首页>Use code first
Use code first
2022-07-16 08:04:00 【Hometown 2130】
Use EF Core establish Code First, Complete database migration .
Use sqlite database
1. Build a .net core web api project

2. install Microsoft.EntityFrameworkCore.Sqlite

3. Create an entity

public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int age { get; set; }
}4. establish DataContext
Inherit DbContext, Initialize constructor , Inherit base, And then User Class write

public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options)
: base(options)
{
}
public DbSet<User> Users { get; set; }
}5. Configuration database

var connrection = "FileName=./demo.db";
services.AddDbContext<DataContext>(options =>
{
options.UseSqlite(connrection);
});Here we can put Sqlite, Change to another database address
Be careful : It must be written here FileName=./demo.db Otherwise, an error will be reported when it is generated later
6. install Microsoft.EntityFrameworkCore.tools

7. To configure appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"tools": {
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-previewl-final",
"imports": [
"portable-net 45+win8+dnxcore50",
"portable-net 45+win8"
]
}
}
}
8. In the package manager console , Input Add-Migration xxx

9. In the input Update-Database, Create database complete


9. Use
Don't forget context.SaveChanges();

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication6.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
private readonly DataContext context;
public WeatherForecastController(ILogger<WeatherForecastController> logger,DataContext context)
{
_logger = logger;
this.context = context;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
User user = new User();
user.age = 15;
user.Id = 1;
user.Name = " Zhang San ";
context.Add(user);
context.SaveChanges();
var rng = new Random();
var n = context.Users.Where(n => n.Name == " Zhang San ");
foreach (var item in n)
{
Console.WriteLine(item.Name);
}
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}
边栏推荐
猜你喜欢

CCF 201909-1 scale detection point query

YoloV1~YoloV4

Prism modularization

老板招了个有6年经验的测试员,让我见识到了什么是天花板...

Thoroughly understand how Kube scheduler completes scheduling

入行软件测试之后,如何持续精进?

TestCafe介绍

Day 13 of leetcode + day 3 of DL

Are you still using the strategy mode to solve if else? Map+ functional interface method is yyds

Reptile Youdao translation
随机推荐
Day 4 of DL
Day 10 of leetcode
简单的canvas动画原理
prism导航功能
.net 5使用LogDashboard
Recursive implementation of combinatorial model
Create eks cluster on AWS
Nodejs uses Multe to upload files and store them on the server
Summary of word segmentation tools
Heap and heap sort
Redux基本语法
I2C协议
YoloV1~YoloV4
CCF 202012-2 optimal threshold for period end forecast
HCIA review
jsonp原理
Azkaban概述
C# .Net Core and Net5 Skills record
数组或是对象、日期的操作
微信小程序开发—(十一)navigator