Introduction
An Object-Relational Mapping (ORM) framework for.NET called Entity Framework Core (EF Core) enables us to work with databases using.NET objects. Stored procedures are supported by EF Core, even though its primary focus is on entity modeling and LINQ-based query capabilities.
Entity Framework Core's stored procedures give users the ability to have predefined SQL logic run on the database server. Performance may gain from this, particularly for sophisticated queries.
Stored Procedure in the EF Core
Depending on whether your stored procedure executes a command, returns data, or performs an INSERT, UPDATE, DELETE, and SELECT there are a few different ways to call it in Entity Framework Core.
As an illustration
I will create a new Empty Asp.Net Core Web API project.
Prerequisites
We need to install below below-mentioned packages to the application.
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.SqlServer
Set up the DbContext
Here is the code for the Emp. cs class.
public class Emp
{
public Guid Id { get; set; }
public string Name { get; set; }
public EmployeeType Type { get; set; }
public string Mno { get; set; }
public decimal Salary { get; set; }
}
Here is the code for the EmpDBContext.cs class.
public class EmpDBContext : DbContext
{
public EmpDBContext(DbContextOptions<EmpDBContext> dbContextOptions)
: base(dbContextOptions)
{
}
public DbSet<Emp> Emps { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
As you can see, we have a matching table in the database represented by the Emps DbSet.
We are developing a constructor that takes a parameter called DbContextOptions. This will allow us to pass options to the Startup class.
builder.Services.AddDbContext<EmpDBContext>(
options => options.UseSqlServer(builder.Configuration.GetConnectionString("SqlServer")));
We're going to add the following connection string to appsettings.json so that I can communicate with the database:
"ConnectionStrings": {
"SqlServer": "Data Source=server_name;Initial Catalog=EmpDB;Integrated Security=True;TrustServerCertificate=True"
}
Constructing a generic repository
Similar to Entity Framework 6, DbContext is used in EF Core to query a database and aggregate changes that are to be written back to the store collectively.
The fantastic thing about the DbContext class is that it supports generics on methods that we'll use to communicate with the database because it is generic.
Here is the code for the IRepository.cs class.
public interface IRepository
{
IQueryable<T> GetAll<T>(string sql);
T Get<T>(string sql, params object[] parameters);
int Create(string sql, params object[] parameters);
T Create<T>(string sql, params object[] parameters);
int Update(string sql, params object[] parameters);
bool Delete(string sql, params object[] parameters);
}
Here is the code for the implementation of the IRepository interface.
public class Repository : IRepository
{
private readonly EmpDBContext _empDBContext;
public Repository(EmpDBContext empDBContext)
{
_empDBContext = empDBContext;
}
public int Create(string sql, params object[] parameters)
{
var result = _empDBContext.Database.ExecuteSqlRaw(sql, parameters);
return result;
}
public T Create<T>(string sql, params object[] parameters)
{
var result = _empDBContext.Database.SqlQueryRaw<T>(sql, parameters);
return result.AsEnumerable().FirstOrDefault()!;
}
public bool Delete(string sql, params object[] parameters)
{
var result = _empDBContext.Database.ExecuteSqlRaw(sql, parameters);
return result > 0;
}
public T Get<T>(string sql, params object[] parameters)
{
var result = _empDBContext.Database.SqlQueryRaw<T>(sql, parameters);
ArgumentNullException.ThrowIfNull(nameof(result));
return result.AsEnumerable().FirstOrDefault()!;
}
public IQueryable<T> GetAll<T>(string sql)
{
return _empDBContext.Database.SqlQueryRaw<T>(sql);
}
public int Update(string sql, params object[] parameters)
{
return _empDBContext.Database.ExecuteSqlRaw(sql, parameters);
}
}
Let's develop the Employee repository interface.
public interface IEmployeeRepository : IRepository
{
//TODO: Write here custom methods that are required for specific requirements.
bool Any(Expression<Func<Emp, bool>> predicate);
}
Since this inherits from the IRepository interface, all of these methods must be implemented.
All those methods from IRepository will be covered, though, if we build an EmployeeRepository that derives from Repository. Furthermore, we must implement IEmployeeRepository.
Here's how it would appear:




Sergei AvraimovPosted Jul 16, 2024, 3:30 PM
Shouldn't we be using async methods to interact with the database?