EF6SQLiteTutorial/Program.cs
| using EF6SQLiteTutorial.Data; |
| |
| using Microsoft.EntityFrameworkCore; |
| |
| var builder = WebApplication.CreateBuilder(args); |
| |
| |
| |
| builder.Services.AddControllers(); |
| |
| builder.Services.AddEndpointsApiExplorer(); |
| builder.Services.AddSwaggerGen(); |
| |
| |
| |
| |
| |
| |
| |
| var dbPath = Path.Combine(builder.Environment.ContentRootPath, "mydatabase.db"); |
| builder.Services.AddDbContext<DataContext>(options => |
| options.UseSqlite($"Data Source={dbPath}")); |
| |
| |
| var app = builder.Build(); |
| |
| |
| if (app.Environment.IsDevelopment()) |
| { |
| app.UseSwagger(); |
| app.UseSwaggerUI(); |
| } |
| |
| app.UseHttpsRedirection(); |
| |
| app.UseAuthorization(); |
| |
| app.MapControllers(); |
| |
| app.Run(); |
| |
EF6SQLiteTutorial/Models/RpgCharacter.cs
| namespace EF6SQLiteTutorial.Models |
| { |
| |
| |
| |
| |
| |
| |
| |
| public class RpgCharacter |
| { |
| public int Id { get; set; } |
| public string Name { get; set; } = string.Empty; |
| public string RpgClass { get; set; } = string.Empty; |
| public int HitPoints { get; set; } = 100; |
| } |
| } |
| |
EF6SQLiteTutorial/Models/WeatherForecast.cs
| namespace EF6SQLiteTutorial.Models |
| { |
| public class WeatherForecast |
| { |
| public DateTime Date { get; set; } |
| |
| public int TemperatureC { get; set; } |
| |
| public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); |
| |
| public string? Summary { get; set; } |
| } |
| } |
EF6SQLiteTutorial/Controllers/RpgCharacterController.cs
| using EF6SQLiteTutorial.Data; |
| using EF6SQLiteTutorial.Models; |
| using Microsoft.AspNetCore.Http; |
| using Microsoft.AspNetCore.Mvc; |
| using Microsoft.EntityFrameworkCore; |
| |
| namespace EF6SQLiteTutorial.Controllers |
| { |
| |
| |
| |
| |
| |
| [Route("api/[controller]")] |
| [ApiController] |
| public class RpgCharacterController : ControllerBase |
| { |
| private readonly DataContext _context; |
| |
| |
| |
| |
| |
| |
| |
| public RpgCharacterController(DataContext context) |
| { |
| _context = context; |
| } |
| |
| |
| |
| |
| |
| |
| [HttpPost] |
| public async Task<ActionResult<List<RpgCharacter>>> AddCharacter(RpgCharacter character) |
| { |
| _context.RpgCharacters.Add(character); |
| await _context.SaveChangesAsync(); |
| |
| |
| |
| |
| |
| |
| return Ok(await _context.RpgCharacters.ToListAsync()); |
| } |
| |
| [HttpGet] |
| public async Task<ActionResult<List<RpgCharacter>>> GetAllCharacters() |
| { |
| return Ok(await _context.RpgCharacters.ToListAsync()); |
| } |
| |
| [HttpGet("{id}")] |
| public async Task<ActionResult<RpgCharacter>> GetCharacter(int id) |
| { |
| var character = await _context.RpgCharacters.FindAsync(id); |
| if (character == null) |
| { |
| return BadRequest("Character not found."); |
| } |
| |
| return Ok(character); |
| } |
| } |
| } |
| |
EF6SQLiteTutorial/Controllers/WeatherForecastController.cs
| using Microsoft.AspNetCore.Mvc; |
| using EF6SQLiteTutorial.Models; |
| |
| |
| namespace EF6SQLiteTutorial.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; |
| |
| public WeatherForecastController(ILogger<WeatherForecastController> logger) |
| { |
| _logger = logger; |
| } |
| |
| [HttpGet(Name = "GetWeatherForecast")] |
| public IEnumerable<WeatherForecast> Get() |
| { |
| return Enumerable.Range(1, 5).Select(index => new WeatherForecast |
| { |
| Date = DateTime.Now.AddDays(index), |
| TemperatureC = Random.Shared.Next(-20, 55), |
| Summary = Summaries[Random.Shared.Next(Summaries.Length)] |
| }) |
| .ToArray(); |
| } |
| } |
| } |
EF6SQLiteTutorial/Data/DataContext.cs
| using Microsoft.EntityFrameworkCore; |
| using EF6SQLiteTutorial.Models; |
| |
| |
| |
| namespace EF6SQLiteTutorial.Data |
| { |
| public class DataContext : DbContext |
| { |
| public DataContext(DbContextOptions<DataContext> options) : base(options) |
| { |
| |
| } |
| |
| public DbSet<RpgCharacter> RpgCharacters => Set<RpgCharacter>(); |
| } |
| } |
| |
EF6SQLiteTutorial/appsettings.json
| { |
| "ConnectionStrings": { |
| "DefaultConnection": "Data Source=sqlite.db" |
| }, |
| "Logging": { |
| "LogLevel": { |
| "Default": "Information", |
| "Microsoft.AspNetCore": "Warning" |
| } |
| }, |
| "AllowedHosts": "*" |
| } |
| |
EF6SQLiteTutorial/EF6SQLiteTutorial.csproj
| <Project Sdk="Microsoft.NET.Sdk.Web"> |
| |
| <PropertyGroup> |
| <TargetFramework>net6.0</TargetFramework> |
| <Nullable>enable</Nullable> |
| <ImplicitUsings>enable</ImplicitUsings> |
| </PropertyGroup> |
| |
| <ItemGroup> |
| <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.8" /> |
| <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.8"> |
| <PrivateAssets>all</PrivateAssets> |
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
| </PackageReference> |
| <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.8" /> |
| <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> |
| </ItemGroup> |
| |
| </Project> |
| |
EF6SQLiteTutorial/appsettings.Development.json
| { |
| "Logging": { |
| "LogLevel": { |
| "Default": "Information", |
| "Microsoft.AspNetCore": "Warning" |
| } |
| } |
| } |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战