Advertisement

Advertisement

Cascading drop down list in asp.net core web api and mvc with repository design pattern n db 1st approach

Cascading drop down list in asp.net core


Step 1. Create asp.net core web api project.

Step 2. Install nuget package.

            EntityFrameworkCore

            EntityFrameworkCore.SqlServer

            EntityFrameworkCore.Tools

EntityFrameworkCore.Design

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />

<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0">

 <PrivateAssets>all</PrivateAssets>

 <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

</PackageReference>


Step 3. Thru db 1st approach scaffold in PCM

Scaffold-dbContext

"Server=DESKTOP-9M9EDAA\\SQLEXPRESS; Database=DbAbpDec; Trusted_Connection= True; MultipleActiveResultSets=True; TrustServerCertificate=True;" Microsoft.EntityFrameworkCore.SqlServer -OutpuDir Models

After getting the auto-generated DbContext file delete the warning message(connection string) .

Step 4:  Add connection string in appsetting.json file 

,
    "ConnectionStrings": {
        "DefaultConnection": "Server = DESKTOP-9M9EDAA\\SQLEXPRESS; Database = DbAbpDec; Trusted_Connection = True; MultipleActiveResultSets = True; TrustServerCertificate = True;"
    }

Step 5. After scaffolding getting models and dbcontext file.


Step 6. Make a Repo folder n in repo IRepository.cs and Repository.cs file


IRepository.cs

public interface IRepository<T> where T : class { Task<IEnumerable<T>> GetAllAsync(); Task<T> GetByIdAsync(int id); Task<T> AddAsync(T entity); Task<T> UpdateAsync(T entity); Task<bool> DeleteAsync(int id); }

Repository.cs

public class Repository<T> : IRepository<T> where T : class { private readonly DbAbpDecContext _db; private readonly DbSet<T> _dbSet; public Repository(DbAbpDecContext db) { _db = db; _dbSet = _db.Set<T>(); } // Get All Records public async Task<IEnumerable<T>> GetAllAsync() { return await _dbSet.ToListAsync(); } // Get a Single Record by ID public async Task<T> GetByIdAsync(int id) { return await _dbSet.FindAsync(id); } // Add a New Record public async Task<T> AddAsync(T item) { await _dbSet.AddAsync(item); await _db.SaveChangesAsync(); return item; } // Update an Existing Record public async Task<T> UpdateAsync(T item) { _dbSet.Update(item); await _db.SaveChangesAsync(); return item; } // Delete a Record by ID public async Task<bool> DeleteAsync(int id) { // Fetch the entity to delete var item = await _dbSet.FindAsync(id); if (item == null) { return false; // Entity not found } _dbSet.Remove(item); await _db.SaveChangesAsync(); return true; // Successfully deleted } }
Cascading drop down list in asp.net core web api and mvc with repository design pattern n db 1st approach Cascading drop down list in asp.net core web api and mvc with repository design pattern n db 1st approach Reviewed by Rikesh on January 28, 2025 Rating: 5

No comments:

Powered by Blogger.