国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

asp.net core 6.0 efcore +sqlserver增刪改查的demo

這篇具有很好參考價值的文章主要介紹了asp.net core 6.0 efcore +sqlserver增刪改查的demo。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

asp.net core 6.0 efcore +sqlserver增刪改查的demo

下面是一個使用ASP.NET Core 5.0和Entity Framework Core進(jìn)行增刪改查操作的示例。

首先,創(chuàng)建一個空的ASP.NET Core 6.0 Web應(yīng)用程序項目。

然后,安裝以下NuGet包:

Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
接下來,創(chuàng)建一個數(shù)據(jù)庫上下文類,用于定義實(shí)體類和數(shù)據(jù)庫連接配置。在項目中創(chuàng)建一個名為AppDbContext.cs的文件,并添加以下代碼:

using Microsoft.EntityFrameworkCore;

namespace EFCoreDemo.Models
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {
        }

        public DbSet<Customer> Customers { get; set; }
    }
}

然后,創(chuàng)建一個實(shí)體類來表示數(shù)據(jù)庫表。在項目中創(chuàng)建一個名為Customer.cs的文件,并添加以下代碼:

namespace EFCoreDemo.Models
{
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }
}

接下來,配置數(shù)據(jù)庫連接。打開appsettings.json文件,并添加以下內(nèi)容:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=EFCoreDemo;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

然后,在Startup.cs文件的ConfigureServices方法中添加以下代碼,用于配置數(shù)據(jù)庫上下文的依賴注入:

services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

接下來,創(chuàng)建一個控制器類來處理增刪改查操作。在項目中創(chuàng)建一個名為CustomersController.cs的文件,并添加以下代碼:

using EFCoreDemo.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace EFCoreDemo.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class CustomersController : ControllerBase
    {
        private readonly AppDbContext _dbContext;

        public CustomersController(AppDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        [HttpGet]
        public async Task<ActionResult<IEnumerable<Customer>>> GetCustomers()
        {
            return await _dbContext.Customers.ToListAsync();
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<Customer>> GetCustomer(int id)
        {
            var customer = await _dbContext.Customers.FindAsync(id);

            if (customer == null)
            {
                return NotFound();
            }

            return customer;
        }

        [HttpPost]
        public async Task<ActionResult<Customer>> CreateCustomer(Customer customer)
        {
            _dbContext.Customers.Add(customer);
            await _dbContext.SaveChangesAsync();

            return CreatedAtAction(nameof(GetCustomer), new { id = customer.Id }, customer);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateCustomer(int id, Customer customer)
        {
            if (id != customer.Id)
            {
                return BadRequest();
            }

            _dbContext.Entry(customer).State = EntityState.Modified;

            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_dbContext.Customers.Any(c => c.Id == id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteCustomer(int id)
        {
            var customer = await _dbContext.Customers.FindAsync(id);

            if (customer == null)
            {
                return NotFound();
            }

            _dbContext.Customers.Remove(customer);
            await _dbContext.SaveChangesAsync();

            return NoContent();
        }
    }
}

最后,運(yùn)行應(yīng)用程序,并使用工具(例如Postman)測試增刪改查操作。以下是一些示例請求的URL和請求體:

GET /api/customers:獲取所有客戶
GET /api/customers/{id}:根據(jù)ID獲取客戶
POST /api/customers:創(chuàng)建客戶 請求體:
json
{
“name”: “John Doe”,
“email”: “john@example.com”
}
PUT /api/customers/{id}:更新客戶 請求體:
json
{
“id”: 1,
“name”: “John Doe”,
“email”: “john.doe@example.com”
}
DELETE /api/customers/{id}:刪除客戶
希望這個示例能幫助你開始使用ASP.NET Core 6.0和Entity Framework Core進(jìn)行增刪改查操作。文章來源地址http://www.zghlxwxcb.cn/news/detail-642342.html

到了這里,關(guān)于asp.net core 6.0 efcore +sqlserver增刪改查的demo的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • ASP.Net Core Web Api+EFCore+MySql實(shí)現(xiàn)動態(tài)查詢(保姆教學(xué))

    ASP.Net Core Web Api+EFCore+MySql實(shí)現(xiàn)動態(tài)查詢(保姆教學(xué))

    本文會詳細(xì)講解如何從打開文件到第一個API開發(fā)完成,過程十分詳細(xì),是基于學(xué)習(xí)入門。 現(xiàn)在讓我們開始吧! 打開VS(演示用的Visual Studio2022) 第一步我們選擇創(chuàng)建新項目? ?第二步 選擇開發(fā)語言以及應(yīng)用程序 我們選擇C# -所有平臺-Web?API.找到 ASP.NET Core Web API 應(yīng)用 ? 這里應(yīng)用

    2024年02月12日
    瀏覽(18)
  • asp.net core框架搭建1-搭建webapi,對數(shù)據(jù)增刪改查接口模板(附源碼)

    asp.net core框架搭建1-搭建webapi,對數(shù)據(jù)增刪改查接口模板(附源碼)

    作者:xcLeigh 文章地址:https://blog.csdn.net/weixin_43151418/article/details/131458922 asp.net core 框架搭建2-搭建webapi ,本文章介紹asp.net core webapi框架搭建,然后開發(fā)增刪改查和工具接口,將一步步帶著大家,實(shí)現(xiàn)目標(biāo)。所有操作過程將展現(xiàn)在本篇文章,下面咋們一起來實(shí)現(xiàn)它吧。 asp.ne

    2024年02月13日
    瀏覽(21)
  • .net core 多項目中使用EFCore

    .net core 多項目中使用EFCore

    類庫一級項目使用.net core 3.1 框架 其中EFCore是和數(shù)據(jù)庫交互的 MultiCore 注入EFCore中的DBContext與數(shù)據(jù)庫交互 主要為了解決多項目中數(shù)據(jù)庫遷移失敗問題 EFCore 工程安裝如下包 MultiCore 安裝如下 EFCore person.cs personconfig.cs EFDbcontext.cs EFDbContextFac .cs 這是關(guān)鍵,但是這僅僅在開發(fā)環(huán)境下

    2024年02月07日
    瀏覽(21)
  • Asp.Net 6.0集成 Log4Net

    Asp.Net 6.0集成 Log4Net

    需要安裝NuGet包,明細(xì)如下: log4net Microsoft.Extensions.Logging.Log4Net.AspNetCore 文件名稱 log4net.config ,默認(rèn)可以放在與啟動類 Program.cs 同級目錄下 在啟動類中進(jìn)行配置(Program.cs)

    2024年02月07日
    瀏覽(26)
  • abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——模塊管理升級(六十)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——模塊管理升級(六十)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——ABP總體介紹(一) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——解決方案介紹(二) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——領(lǐng)域?qū)觿?chuàng)建實(shí)體(三) ? abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——定義倉儲并實(shí)現(xiàn) (四) abp(net core)+easyui+efcore實(shí)

    2023年04月09日
    瀏覽(92)
  • 【C#】.net core 6.0 依賴注入生命周期

    【C#】.net core 6.0 依賴注入生命周期

    給自己一個目標(biāo),然后堅持一段時間,總會有收獲和感悟! 對于.net core而言,依賴注入生命周期有三種瞬態(tài)(Transient)、作用域(Scoped)和單例(Singleton),無論使用哪種生命周期,都需要確保對象的線程安全性,并正確地處理依賴關(guān)系。 在了解依賴注入的生命周期前,我

    2024年02月03日
    瀏覽(89)
  • .NET6 + EF Core + MySQL 創(chuàng)建實(shí)體和數(shù)據(jù)庫、EFCore 數(shù)據(jù)遷移

    .NET6 + EF Core + MySQL 創(chuàng)建實(shí)體和數(shù)據(jù)庫、EFCore 數(shù)據(jù)遷移

    接上期文章《.NET6項目連接數(shù)據(jù)庫方式方法》,有人問了我?guī)讉€問題,現(xiàn)在就這幾個問題,拓展延申一下創(chuàng)建實(shí)體類、數(shù)據(jù)庫。把ORM框架和數(shù)據(jù)遷移都寫進(jìn)去。 我的項目是在Linux上創(chuàng)建的,使用的是vscode開發(fā)工具遠(yuǎn)程開發(fā)。為了方便大家閱讀和操作,我將項目down到我的本地電

    2024年02月05日
    瀏覽(24)
  • .net core 6.0 web api 爬坑日記

    本人以前做前端開發(fā)的,就是 html , js , css , vue , react 那些 此前沒接觸過 .net 甚至沒接觸過 C# , 若哪里不對或有缺陷歡迎指出,以便改正! ^_^ 這是當(dāng)前所在公司的一個小項目 , 雖然這個項目不大, 但是奈何我又喜歡新版本,所以直接用的 .net core 6.0 而且多數(shù)問題百度, 谷歌 都只有

    2024年02月04日
    瀏覽(53)
  • abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——組織管理升級之下(六十二)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——組織管理升級之下(六十二)

    A bp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)目錄 abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——ABP總體介紹(一) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——解決方案介紹(二) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——領(lǐng)域?qū)觿?chuàng)建實(shí)體(三) ? abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——

    2023年04月23日
    瀏覽(50)
  • abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——模塊管理升級之上(六十一)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——模塊管理升級之上(六十一)

    ? A bp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)目錄 abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——ABP總體介紹(一) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——解決方案介紹(二) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)——領(lǐng)域?qū)觿?chuàng)建實(shí)體(三) ? abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲管理系統(tǒng)—

    2023年04月16日
    瀏覽(57)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包