We learn ASP.NET Core Web API 6.0 With EF & SQL RepositoryPattern,
STEP 1
Create ASP.NET Core Web API Project

STEP 2

STEP 3

STEP 4
Create Table in SQL
CREATE TABLE [dbo].[Tbl_Account](
[AC_ID] [bigint] IDENTITY(1,1) NOT NULL,
[AC_Name] [varchar](max) NULL,
[AC_Number] [nvarchar](max) NULL,
[AC_isactive] [bit] NULL,
PRIMARY KEY CLUSTERED
(
[AC_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Step 5
Create Procedure in SQL
CREATE proc [dbo].[Proc_InsertAccount]
(
@AC_ID bigint=null,
@AC_Name varchar(max)=null,
@AC_Number varchar(max)=null,
@AC_Isactive bit=null,
@Flag varchar(max)=null,
@AC_IDOutput bigint output
)
as
begin
IF(@Flag ='IN')
BEGIN
insert into Tbl_Account(AC_Name,AC_Number,AC_isactive)
values (@AC_Name,@AC_Number,@AC_Isactive)
SET @AC_IDOutput = @@IDENTITY
END
ELSE IF(@Flag='UP')
BEGIN
UPDATE Tbl_Account SET AC_Name=@AC_Name,AC_Number=@AC_Number,AC_isactive=@AC_isactive WHERE AC_ID=@AC_ID
SET @AC_IDOutput = @AC_ID
END
ELSE IF(@Flag='DE')
BEGIN
DELETE FROM Tbl_Account WHERE AC_ID=@AC_ID
END
end
Step 6
CREATE Proc [dbo].[Proc_Account]
as
select * from Tbl_account
retrun
Step 7
Add EFCoreEntity & EFSql Entity from nuget package
Step 8
Add the Connection string in Appsettings.json file

"ConnectionStrings": {
"Con": "Data Source=SERVER-8777;User ID=sa;Password=8777;Initial Catalog=SqlBankCore;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
}
Step 9
Configure Connection String in Program.cs file
using Microsoft.EntityFrameworkCore;
builder.Services.AddDbContext<DataBaseContext>
(options => options.UseSqlServer(builder.Configuration.GetConnectionString("Con")));
Step 9
Add interface folder under solution & add Interface for Account
using SqlBankApi7.Model;
namespace SqlBankApi7.Interface
{
public interface IAccount
{
public List<Common> GetAccountDetails();
public Common AddAccount(Account account);
public void UpdateAccount(Account account);
public Account DeleteAccount(Account account);
}
}
Step 10
Create model folder under solution & create class Account,
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SqlBankApi7.Model
{
public class Account
{
[Key]
public long AC_ID { get; set; }
public string? AC_Name { get; set; }
public string? AC_Number { get; set; }
public bool AC_isactive { get; set; }
[NotMapped]
public string? Flag { get; set; }
}
}
Step 11
Add DatabaseContext folder under project solution & create Database context class,
using Microsoft.EntityFrameworkCore;
using SqlBankApi7.Model;
namespace SqlBankApi7.DataBaseContext
{
public partial class DataBaseContext : DbContext
{
public DataBaseContext()
{
}
public DataBaseContext(DbContextOptions<DataBaseContext> options)
: base(options)
{
}
public virtual DbSet<Account>? Accounts { get; set; }
public virtual DbSet<User>? Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>(user =>
{
user.HasKey(u => u.UserID);
user.ToTable("Tbl_User");
user.Property(u => u.UserID).HasColumnName("UserID");
user.Property(u => u.DisplayName).HasMaxLength(50).IsUnicode(false);
user.Property(u => u.UserName).HasMaxLength(50).IsUnicode(false);
user.Property(u => u.Email).HasMaxLength(50).IsUnicode(false);
user.Property(u => u.Password).HasMaxLength(50).IsUnicode(false);
user.Property(u => u.CreatedDate).IsUnicode(false);
});
modelBuilder.Entity<Account>(entity =>
{
entity.HasKey(e => e.AC_ID);
entity.ToTable("Tbl_Account");
entity.Property(e => e.AC_Name).HasMaxLength(100).IsUnicode(true);
entity.Property(e => e.AC_Number).HasMaxLength(100).IsUnicode(true);
entity.Property(e => e.AC_isactive).HasMaxLength(100).IsUnicode(true);
OnModelCreatingPartial(modelBuilder);
});
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}
Step 12
We are using Repository Pattern for this core web API. Now create repository folder under solution & create Common class & AccountRepository.cs class
using SqlBankApi7.Model;
using System.ComponentModel.DataAnnotations.Schema;
namespace SqlBankApi7
{
public class Common
{
public List<Account>? Table { get; set; }
[NotMapped]
public string? Message { get; set; }
[NotMapped]
public int StatusCode { get; set; }
[NotMapped]
public string? Status { get; set; }
}
}



panagiotis kartisPosted Jan 30, 2024, 5:56 PM
Full CRUD Operations in Asp Net Core MVC using Entity Framework Core with Repository Pattern
aaron shorePosted Apr 16, 2023, 10:11 AM
Hi Atul, I get the error "System.InvalidOperationException: Unable to resolve service for type 'Api.Service.Identity.Services.IIdentityService' while attempting to activate 'Api.Service.Identity.Controllers.IdentityController'." when trying to use your tutorial.