EFcore 依赖注入

2023-10-21 23:08
文章标签 依赖 注入 efcore

本文主要是介绍EFcore 依赖注入,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

基于EFcore 采用DBFirst模式 实现DBContext依赖注入

1.SQL Server创建数据库

创建一个名为Example的数据库,并含有UserInfo、Contacts数据表

UserInfo字段:
在这里插入图片描述
Contacts字段
在这里插入图片描述

2.VS引入Nuget程序包

在这里插入图片描述
可以在程序包管理控制台 手动键入命令行安装Nuget包:

install-package microsoft.entityframeworkcore
install-package microsoft.entityframeworkcore.sqlserver
install-package mircosoft.entityframeworkcore.tools

3.Scaffold-DbContext框架生成Model

Scaffold-DBContext “server=.;database=Example;uid=sa;pwd=sa123456” microsoft.entityframeworkcore.sqlserver -outputDir Models

命令行说明:
在这里插入图片描述
框架Build Succeed后,自动生成数据库实体类与DbContext类

UserInfo:

namespace WebApplication24.Models
{public partial class UserInfo{public int Id { get; set; }public string Name { get; set; }public int? Age { get; set; }}
}

Contact:

namespace WebApplication24.Models
{public partial class Contact{public int Id { get; set; }public string Name { get; set; }public string Phone { get; set; }public string Address { get; set; }}
}

ExampleContext:

namespace WebApplication24.Models
{public partial class ExampleContext : DbContext{public ExampleContext(){}public ExampleContext(DbContextOptions<ExampleContext> options): base(options){}public virtual DbSet<Contact> Contacts { get; set; }public virtual DbSet<UserInfo> UserInfos { get; set; }protected override void OnModelCreating(ModelBuilder modelBuilder){modelBuilder.HasAnnotation("Relational:Collation", "Chinese_PRC_CI_AS");modelBuilder.Entity<Contact>(entity =>{entity.Property(e => e.Id).ValueGeneratedNever().HasColumnName("ID");entity.Property(e => e.Address).HasMaxLength(50);entity.Property(e => e.Name).HasMaxLength(50);entity.Property(e => e.Phone).HasMaxLength(50);});modelBuilder.Entity<UserInfo>(entity =>{entity.ToTable("UserInfo");entity.Property(e => e.Id).ValueGeneratedNever().HasColumnName("ID");entity.Property(e => e.Name).HasMaxLength(50);});OnModelCreatingPartial(modelBuilder);}partial void OnModelCreatingPartial(ModelBuilder modelBuilder);}
}

4.依赖注入DBContext

Startup类中依赖注入

public void ConfigureServices(IServiceCollection services)
{services.AddControllersWithViews();services.AddDbContext<ExampleContext>(options => {options.UseSqlServer(Configuration.GetConnectionString("defaultConnection"));});
}

appsettings.json中加入连接字符串

"ConnectionStrings": {"defaultConnection": "server=.;database=example;uid=sa;pwd=sa123456"}

AddDbContext解释摘要:

摘要://     Registers the given context as a service in the Microsoft.Extensions.DependencyInjection.IServiceCollection.//     Use this method when using dependency injection in your application, such as//     with ASP.NET Core. For applications that don't use dependency injection, consider//     creating Microsoft.EntityFrameworkCore.DbContext instances directly with its//     constructor. The Microsoft.EntityFrameworkCore.DbContext.OnConfiguring(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder)//     method can then be overridden to configure a connection string and other options.//     For more information on how to use this method, see the Entity Framework Core//     documentation at https://aka.ms/efdocs. For more information on using dependency//     injection, see https://go.microsoft.com/fwlink/?LinkId=526890.

5.DBContext构造函数注入

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using WebApplication24.Models;namespace WebApplication24.Controllers
{public class HomeController : Controller{private readonly ILogger<HomeController> _logger;private readonly ExampleContext _db;public HomeController(ILogger<HomeController> logger,ExampleContext exampleContext){_logger = logger;_db = exampleContext;}public IActionResult Index(){var query = _db.UserInfos.Find(1);return View();}      }
}

这篇关于EFcore 依赖注入的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/257519

相关文章

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Python依赖库的几种离线安装方法总结

《Python依赖库的几种离线安装方法总结》:本文主要介绍如何在Python中使用pip工具进行依赖库的安装和管理,包括如何导出和导入依赖包列表、如何下载和安装单个或多个库包及其依赖,以及如何指定... 目录前言一、如何copy一个python环境二、如何下载一个包及其依赖并安装三、如何导出requirem

Python如何快速下载依赖

《Python如何快速下载依赖》本文介绍了四种在Python中快速下载依赖的方法,包括使用国内镜像源、开启pip并发下载功能、使用pipreqs批量下载项目依赖以及使用conda管理依赖,通过这些方法... 目录python快速下载依赖1. 使用国内镜像源临时使用镜像源永久配置镜像源2. 使用 pip 的并

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

Spring AI Alibaba接入大模型时的依赖问题小结

《SpringAIAlibaba接入大模型时的依赖问题小结》文章介绍了如何在pom.xml文件中配置SpringAIAlibaba依赖,并提供了一个示例pom.xml文件,同时,建议将Maven仓... 目录(一)pom.XML文件:(二)application.yml配置文件(一)pom.xml文件:首

使用maven依赖详解

《使用maven依赖详解》本文主要介绍了Maven的基础知识,包括Maven的简介、仓库类型、常用命令、场景举例、指令总结、依赖范围、settings.xml说明等,同时,还详细讲解了Maven依赖的... 目录1. maven基础1.1 简介1.2 仓库类型1.3 常用命令1.4 场景举例1.5 指令总结

SQL注入漏洞扫描之sqlmap详解

《SQL注入漏洞扫描之sqlmap详解》SQLMap是一款自动执行SQL注入的审计工具,支持多种SQL注入技术,包括布尔型盲注、时间型盲注、报错型注入、联合查询注入和堆叠查询注入... 目录what支持类型how---less-1为例1.检测网站是否存在sql注入漏洞的注入点2.列举可用数据库3.列举数据库

Spring核心思想之浅谈IoC容器与依赖倒置(DI)

《Spring核心思想之浅谈IoC容器与依赖倒置(DI)》文章介绍了Spring的IoC和DI机制,以及MyBatis的动态代理,通过注解和反射,Spring能够自动管理对象的创建和依赖注入,而MyB... 目录一、控制反转 IoC二、依赖倒置 DI1. 详细概念2. Spring 中 DI 的实现原理三、

python中poetry安装依赖

《python中poetry安装依赖》本文主要介绍了Poetry工具及其在Python项目中的安装和使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录前言1. 为什么pip install poetry 会造成依赖冲突1.1 全局环境依赖混淆:1