hacklink al hack forum organik hit kayseri escort b-ok zeta library books z librarygrandpashabetistanbul escortsfixbetMegabahiszbahistipobetgamdombetciodinamobetsahabetbetmatik güncel girişbetsat güncel girişanal sex pornviagraanal sexvaycasinovaycasino girişbelugabahisbelugabahisvbetvbet girişkralbetbetebetbetgarantidinamobetalobetluxbet girişanal pornpornhubfixbetjojobet 1030 com giris

Post Details Page

At Devhq.in, we provide comprehensive digital solutions for businesses. From software development and app creation to social media management and brand design, we help you build and grow your digital presence.

How to Create Read Update and Delete student in Asp dot net web api

First we have to make same file and folders

📁 In IStudent.cs

using Univercity.Models;

namespace Univercity.Services.Interfaces
{
    public interface IStudent
    {
        Task<List<Student>> GetStudents();
        Task<Student> GetStudent(string id);
        Task<Student> CreateStudent(Student student);
        Task UpdateStudent(string id, Student studentIn);
        Task DeleteStudent(string id);
    }
}
C#

This is all are interface for adding and deleting data from mongodb database

Now Go to 📁StudentService.cs

Where we will write all serivce for student and also interface implementation will there

using MongoDB.Driver;
using Univercity.DbContext;
using Univercity.Models;
using Univercity.Services.Interfaces;

namespace Univercity.Services
{
    public class StudentServices : IStudent
    {
        private readonly IMongoCollection<Student> _students;
        public StudentServices(MongoDbContext context)
        {
            _students = context.StudentDb;
        }

        public Task<Student> CreateStudent(Student student)
        {
            throw new NotImplementedException();
        }

        public Task DeleteStudent(string id)
        {
            throw new NotImplementedException();
        }

        public Task<Student> GetStudent(string id)
        {
            throw new NotImplementedException();
        }

        public Task<List<Student>> GetStudents()
        {
            throw new NotImplementedException();
        }

        public Task UpdateStudent(string id, Student studentIn)
        {
            throw new NotImplementedException();
        }
    }
}
C#

Selected line are the connection of database

🧑‍💻 Create Student Function

 public async  Task<Student> CreateStudent(Student student)
 {
     await _students.InsertOneAsync(student);
     return student;
 }
C#

🧑‍💻 Get All Students Function

  public async Task<List<Student>> GetStudents()
  {
      var students = await _students.Find(e=>true).ToListAsync();
      return students;
  }
C#

🧑‍💻 Get One Student Function

   public async Task<Student> GetStudent(string id)
   {
       var student = await _students.Find<Student>(e=>e.Id==id).FirstOrDefaultAsync();
       return student;
   }
C#

🧑‍💻 Delete One Students Function

  public async Task DeleteStudent(string id)
  {
         await _students.DeleteOneAsync(student => student.Id == id);
  }
C#

🧑‍💻 Update Students Function

 public async  Task UpdateStudent(string id, Student studentIn)
 {
    await _students.ReplaceOneAsync(student => student.Id == id, studentIn);
 }
C#
After adding all function final code will look like this
using MongoDB.Driver;
using Univercity.DbContext;
using Univercity.Models;
using Univercity.Services.Interfaces;

namespace Univercity.Services
{
    public class StudentServices : IStudent
    {
        private readonly IMongoCollection<Student> _students;
        public StudentServices(MongoDbContext context)
        {
            _students = context.StudentDb;
        }

        public async  Task<Student> CreateStudent(Student student)
        {
            await _students.InsertOneAsync(student);
            return student;
        }

        public async Task DeleteStudent(string id)
        {
               await _students.DeleteOneAsync(student => student.Id == id);
        }

        public async Task<Student> GetStudent(string id)
        {
            var student = await _students.Find<Student>(e=>e.Id==id).FirstOrDefaultAsync();
            return student;
        }

        public async Task<List<Student>> GetStudents()
        {
            var students = await _students.Find(e=>true).ToListAsync();
            return students;
        }

        public async  Task UpdateStudent(string id, Student studentIn)
        {
           await _students.ReplaceOneAsync(student => student.Id == id, studentIn);
        }
    }
}
C#

Share now :

Facebook
Twitter
LinkedIn
Review Your Cart
0
Add Coupon Code
Subtotal