当前位置:网站首页>使用Code First
使用Code First
2022-07-13 18:09:00 【故里2130】
使用EF Core建立Code First,完成数据库迁移。
使用sqlite数据库
1.建立一个.net core web api项目

2.安装Microsoft.EntityFrameworkCore.Sqlite

3. 建立一个实体

public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int age { get; set; }
}4.建立DataContext
继承DbContext,初始化构造函数,继承base,再把User类写入

public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options)
: base(options)
{
}
public DbSet<User> Users { get; set; }
}5.配置数据库

var connrection = "FileName=./demo.db";
services.AddDbContext<DataContext>(options =>
{
options.UseSqlite(connrection);
});这里可以把Sqlite,换成别的数据库地址
注意:这里必须写 FileName=./demo.db 否则后面生成的时候报错
6.安装Microsoft.EntityFrameworkCore.tools

7. 配置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.在程序包管理器控制台中,输入Add-Migration xxx

9.在输入Update-Database,创建数据库完成


9.使用
别忘记 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 = "张三";
context.Add(user);
context.SaveChanges();
var rng = new Random();
var n = context.Users.Where(n => n.Name == "张三");
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();
}
}
}
边栏推荐
- It is said that software testing can be done by everyone, but why are there still a large number of people who are discouraged every year?
- Principle and configuration of static routing
- 交换机基本原理与配置
- Edge calculation kubeedge+edgemash
- 64位整除乘法
- Why can't we use redis expiration monitoring to close orders?
- HCIA review
- Heap and heap sort
- "Finally I left bytek..." confession of a test engineer with an annual salary of 40W
- RAID disk array
猜你喜欢

头文件ctype.h(详细)

Looking at "money" ~ the experience of a tester with two years' graduation and an annual salary of 30W

MySQL learning records

The experience of finding a job after coming out of the software testing training class taught me these five things

CCF 202012-2 期末预测之最佳阈值

GoFrame Step by Step Demo - P1

Master-slave copy reading and writing separation nanny level teaching

Linux下安装单机版redis

Is it true that you can't do software testing until you're 35? So what should I do I'm 36

vscode更新时,报错 Failed to install visual Studio Code update. Updates may fail due to anti-virus softwa
随机推荐
How to apply @transactional transaction annotation to perfection?
Longest ascending subsequence longest common subsequence maximum field and longest non repeating subsequence
HCIA review
Day 16 of leetcode
Day 12 of leetcode + day 1 of DL
I2C协议
RAID disk array
LVM and disk quota
Day 6 of leetcode question brushing
nodejs+express设置和获取cookie,session
Day 14 of leetcode
C# .Net Core and Net5 Skills record
C#笔记-基础知识,问答,WPF
Are you still using the strategy mode to solve if else? Map+ functional interface method is yyds
Basic principle and configuration of switch
Principe jsonp
3、 Experimental report on the implementation of SMB sharing and FTP construction by freenas
Day 6 of DL
MATPLOTLIB—fail to allocate bitmap
Azkaban概述