ASP.NET Core中的SQLite与EntityFrameworkCore

时间:2016-04-07 22:39:45

标签: c# asp.net-mvc sqlite asp.net-core-mvc entity-framework-core

如何使用EntityFramework 7在ASP.NET Core Web应用程序中添加和使用SQLite数据库?

当我听到它并创建我的第一个Web应用程序时,我潜入了ASP.NET Core,我突然想要存储一堆数据,而SQLite似乎是显而易见的选择。
因为我希望它与我的应用程序保持一致,所以要保持它的轻量级,简单并避免设置单独的数据库。

那么如何在ASP.NET Core中创建SQLite数据库?

  • ASP.NET Core - 现在以前称为ASP.NET MVC 6
  • EntityFramework Core - 以前称为EntityFramework 7

2 个答案:

答案 0 :(得分:7)

如果您想使用SQLite为数据库创建ASP.NET Core Web应用程序,我强烈建议您使用Yeoman为您构建应用程序。您需要先安装.NET Core 1.1 SDK(Visual Studio 2015目前似乎只包含SDK版本1.0.0和1.0.1)。然后,您需要安装npm附带的Node.js,然后安装以下npm软件包:yogenerator-aspnet。然后,您只需运行yo aspnet并回答几个问题。

C:\Development>yo aspnet
? ==========================================================================
We're constantly looking for ways to make yo better!
May we anonymously report usage statistics to improve the tool over time?
More info: https://github.com/yeoman/insight & http://yeoman.io
========================================================================== Yes

     _-----_     ╭──────────────────────────╮
    |       |    │      Welcome to the      │
    |--(o)--|    │  marvellous ASP.NET Core │
   `---------´   │        generator!        │
    ( _´U`_ )    ╰──────────────────────────╯
    /___A___\   /
     |  ~  |
   __'.___.'__
 ´   `  |° ´ Y `

? What type of application do you want to create? Web Application
? Which UI framework would you like to use? Bootstrap (3.3.6)
? What's the name of your ASP.NET application? WebApplication

之后,您将收到以下回复:

 Your project is now created, you can use the following commands to get going
    cd "WebApplication"
    dotnet restore
    dotnet build (optional, build will also happen when it's run)
    dotnet ef database update (to create the SQLite database for the project)
    dotnet run

运行dotnet restoredotnet ef database update,然后dotnet run并转到localhost:5000以确保项目正在运行。

现在,您可以在Visual Studio 2015中打开项目(假设您使用的是Windows)或Visual Studio Code。

ASP.NET Core web application generated using Yeoman

关于这一点的好处是Startup.csproject.jsonappsettings.json文件设置为使用SQLite。此外,还为您创建了一个SQLite数据库:

<强> Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
}

<强> project.json:

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

<强> appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=WebApplication.db"
  }
}

您的SQLite数据库将位于bin/Debug/netcoreapp1.0。就我而言,它位于C:\Development\WebApplication\bin\Debug\netcoreapp1.0\WebApplication.db

如果要重命名SQLite数据库,请修改appsettings.json文件并运行dotnet ef database update

要了解有关在.NET Core和EF Core中使用SQLite数据库的更多信息,请查看以下文章:.NET Core - New Database

答案 1 :(得分:2)

  1. 安装下面提到的包

     PM> Install-Package Microsoft.EntityFrameworkCore
     PM> Install-Package Microsoft.EntityFrameworkCore.Sqlite
     PM> Install-Package Microsoft.EntityFrameworkCore.Tools
    
  2. 创建模型

  3. 创建 DBContext 类添加 SQLite 连接配置

     protected override void OnConfiguring(DbContextOptionsBuilder options)
         => options.UseSqlite("Data Source=DBFileName.db");
    
  4. 运行迁移命令以开始使用它

     PM> add-migration <MigrationName>  //Ex: add-migration IntialMigration
     PM> update-database
    

https://fullstack-lab.co.in/Sqlite-entity-framework-core-quick-start

本文提供了在 Asp.net core 3.1 中使用 SQLite 的简单步骤