Setup Entity Framework Core in ASP.NET Core

1. Run following command to install required packages:

Install-Package Microsoft.EntityFrameworkCore.SqlServer

2. For database first approach, need to run below two commands to install required tools:

Install-Package Microsoft.EntityFrameworkCore.Tools

Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design

3. Run following command to generate data models:

Scaffold-DbContext "Server=(local);Database=DBName;User ID=sa;Password=password;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force -DataAnnotations -Context "CustomDBContext"

4. Move the connection string to config file:

1) Add connection string in file appsettings.json

{
  "ConnectionStrings": {
    "CustomDBContext": "Server=(local);Database=DBName;User ID=sa;Password=password;"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }

2) Define static DBConnectionString property in DBContext file:

    public partial class CustomDBContext : DbContext
    {
        ...
        public static string DBConnectionString { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(DBConnectionString);
        }
        ...
    }

3) Get database connection string from appsettings.json file and assign it to DBConnectionString in Startup.cs file

        public void ConfigureServices(IServiceCollection services)
        {
            // Config database connection string
            NUHOTRSContext.DBConnectionString = Configuration.GetConnectionString("CustomDBContext");
            ...
        }

参考链接:

https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell

https://joonasw.net/view/asp-net-core-1-configuration-deep-dive

时间: 2024-08-08 17:48:41

Setup Entity Framework Core in ASP.NET Core的相关文章

Entity Framework Tutorial Basics(4):Setup Entity Framework Environment

Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet package and in .NET framework. The .NET framework 4.0/4.5 included EF core API, whereas EntityFramework.dll via NuGet package included EF 5.0 specifi

ASP.NET Core 视图 - ASP.NET Core 基础教程 - 简单教程,简单编程

原文:ASP.NET Core 视图 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 视图 花了几章节,终于把 ASP.NET Core MVC 中的 C 控制器涉及的七七八八了,本章节我们来学习下 V,也就是视图部分. ASP.NET Core MVC 应用程序中,没有任何内容像页面,并且在 URL 中指定路径时, 它也不包含与页面直接对应的任何内容. ASP.NET Core MVC 应用程序中最接近页面的东西被称为视图 是不是很拗口,哈哈,页面就是

在传统.NET Framework 上运行ASP.NET Core项目

新的项目我们想用ASP.NET Core来开发,但是苦于我们历史的遗产很多,比如<使用 JavaScriptService 在.NET Core 里实现DES加密算法>,我们要估计等到.NET Core 1.2我们才会有大部分的API,通过NodeJs方式有点曲线救国的味道,很多人会不认同,如果要在纯的.NET Core下运行我们的ASP.NET Core项目,在特定的时间进度内很难办到,不仅仅我们所依赖的一些API还没有,还有我们大量的.NET Framwork的库还来不及迁移到.NET S

Announcing MSTest Framework support for .NET Core RC2 / ASP.NET Core RC2[译]

??.NET Core RC2 和 ASP.NET Core RC2 已在几周之前发布,它包含了.NET CLI的介绍,对.NET Core SDK(以前叫 DNX)的改动,ASP.NET 5到ASP.NET Core的进化等,更多特性可以参考.NET 团队博客和.NET WEB开发团队博客. ??而现在,我们很兴奋的宣布,MSTest类库也支持这个发布版本啦!相关类库和依赖包都可以在NuGet上获取.这次是预发布版本,我们期望收到你的反馈以便带来更完善的正式版本. ??在这篇文章中,我会向你展

Dotnet Core 在ASP.NET Core中使用静态文件

来自微软官网  在ASP.NET Core中使用静态文件:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/static-files: 提供静态文件 静态文件通常位于web root(<content-root> / wwwroot)文件夹中.有关详细信息,请参阅内容根和Web根.您通常将内容根设置为当前目录,以便web root在开发过程中找到项目. public static void Main(string[] args)

[转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)

本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application Download Completed Project The Contoso University sample web application de

【.NET Core】ASP.NET Core之IdentityServer4(1):快速入门

本文中的IdentityServer4基于上节的jenkins 进行docker自动化部署. 使用了MariaDB,EF Core,AspNetIdentity,Docker Demo地址:https://sso.neverc.cn Demo源码:https://github.com/NeverCL/Geek.IdentityServer4 简介 OpenID Connect :常用的认证协议有SAML2p, WS-Federation and OpenID Connect – SAML2p.O

K8S+GitLab-自动化分布式部署ASP.NET Core(二) ASP.NET Core DevOps

一.介绍 前一篇,写的K8S部署环境的文章,下面正式开始部署ASP.NET Core 项目. 二.正式部署ASP.NET Core项目 GitHub地址: https://github.com/gyw1309631798/Deploy-API. 我创建了一个ASP.NET Core 2.1 WebAPI项目 里面包含了deploy.yaml,Dockerfile文件. 要在K8S上部署首先要添加regsecret ,不然从Harbor pull会失败. kubectl create namesp

使用Visual Studio开发ASP.NET Core MVC and Entity Framework Core初学者教程

原文地址:https://docs.asp.net/en/latest/data/ef-mvc/intro.html The Contoso University sample web application demonstrates how to create ASP.NET Core 1.0 MVC web applications using Entity Framework Core 1.0 and Visual Studio 2015. Contoso University网络应用的案