Create .Net core api project using simple repository pattern Setup
1). Create .Net Core Project Name It DntAppApi
i). Select .Net core web application
ii).select api project
2). Craete .Net core class library project Name it (DntAppApi.Core)
i). Add Microsoft.EntityFrameworkCore package From Nuget Package Manager
ii). Add Microsoft.EntityFrameworkCore.SqlServer package From Nuget Package Manager
3). Craete Another .Net core class library project Name it (DntAppApi.Infrastructure)
i). Add Microsoft.EntityFrameworkCore package From Nuget Package Manager
ii). Open Tools > Nuget package Manager > Package Manager Console.
iii). In Package Manager Console Select DntAppApi.Infrastructure Project's From Default Project Dropdown Selection
iv). Fire Command :-
Scaffold-DbContext "data source=DESKTOP-4VG8BV2\SQLEXPRESS; initial catalog=MP;Integrated Security=true" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entities -F
Note* This will create entity folder containing all table of database as class.
4). Build DntAppApi.Core Project And Add Reference of this Project To DntAppApi.Infrastructure Project
5). Now Build DntAppApi.Infrastructure Project
- Add Reference of DntAppApi.Core And DntAppApi.Infrastructure To DntAppApi(Main) Project
Now add two folders in DntAppApi.Infrastructure Project
1) Abstraction (Service) :- Contains All Interfaces
2).Implementation (Servicecontracts) :- Contains All Class Which Implements Respective Interfaces.
Add an Interfaces in Abstraction folder
==============================================================
=====================IProduct_Repository=======================
==============================================================
namespace DntAppApi
{
public interface IProduct_Repository : IDisposable
{
IQueryable<TblProduct> GetAll();
void Save(TblProduct obj);
void Remove(int Productid);
}
}
Add an Class in Abstraction folder
==============================================================
=====================Product_Repository========================
==============================================================
namespace DntAppApi
{
public class Product_Repository : IProduct_Repository
{
private MPContext context;
public Product_Repository(MPContext _context)
{
context = _context;
}
public IQueryable<TblProduct> GetAll()
{
return context.TblProduct.AsQueryable();
}
public void Save(TblProduct obj)
{
if (obj.Productid == 0)
{
context.TblProduct.Add(obj);
context.SaveChanges();
}
else
{
context.Entry<TblProduct>(obj).State = EntityState.Modified;
context.SaveChanges();
}
}
public void Remove(int Productid)
{
var obj = context.TblProduct.Find(Productid);
context.Entry<TblProduct>(obj).State = EntityState.Deleted;
context.SaveChanges();
}
// Flag: Has Dispose already been called?
bool disposed = false;
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
context.Dispose();
}
// Free any unmanaged objects here.
disposed = true;
}
}
}
Note :- Wherever you get compiler error put cursour over there press (Ctrl + dot) and add
Required Namespace
Till Now Project Setup Is Ready....
.Netcore Api + angular 6 + EFCore Project Part - 2
1). Create .Net Core Project Name It DntAppApi
i). Select .Net core web application
ii).select api project
2). Craete .Net core class library project Name it (DntAppApi.Core)
i). Add Microsoft.EntityFrameworkCore package From Nuget Package Manager
ii). Add Microsoft.EntityFrameworkCore.SqlServer package From Nuget Package Manager
3). Craete Another .Net core class library project Name it (DntAppApi.Infrastructure)
i). Add Microsoft.EntityFrameworkCore package From Nuget Package Manager
ii). Open Tools > Nuget package Manager > Package Manager Console.
iii). In Package Manager Console Select DntAppApi.Infrastructure Project's From Default Project Dropdown Selection
iv). Fire Command :-
Scaffold-DbContext "data source=DESKTOP-4VG8BV2\SQLEXPRESS; initial catalog=MP;Integrated Security=true" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entities -F
Note* This will create entity folder containing all table of database as class.
4). Build DntAppApi.Core Project And Add Reference of this Project To DntAppApi.Infrastructure Project
5). Now Build DntAppApi.Infrastructure Project
- Add Reference of DntAppApi.Core And DntAppApi.Infrastructure To DntAppApi(Main) Project
Now add two folders in DntAppApi.Infrastructure Project
1) Abstraction (Service) :- Contains All Interfaces
2).Implementation (Servicecontracts) :- Contains All Class Which Implements Respective Interfaces.
Add an Interfaces in Abstraction folder
==============================================================
=====================IProduct_Repository=======================
==============================================================
namespace DntAppApi
{
public interface IProduct_Repository : IDisposable
{
IQueryable<TblProduct> GetAll();
void Save(TblProduct obj);
void Remove(int Productid);
}
}
Add an Class in Abstraction folder
==============================================================
=====================Product_Repository========================
==============================================================
namespace DntAppApi
{
public class Product_Repository : IProduct_Repository
{
private MPContext context;
public Product_Repository(MPContext _context)
{
context = _context;
}
public IQueryable<TblProduct> GetAll()
{
return context.TblProduct.AsQueryable();
}
public void Save(TblProduct obj)
{
if (obj.Productid == 0)
{
context.TblProduct.Add(obj);
context.SaveChanges();
}
else
{
context.Entry<TblProduct>(obj).State = EntityState.Modified;
context.SaveChanges();
}
}
public void Remove(int Productid)
{
var obj = context.TblProduct.Find(Productid);
context.Entry<TblProduct>(obj).State = EntityState.Deleted;
context.SaveChanges();
}
// Flag: Has Dispose already been called?
bool disposed = false;
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
context.Dispose();
}
// Free any unmanaged objects here.
disposed = true;
}
}
}
Note :- Wherever you get compiler error put cursour over there press (Ctrl + dot) and add
Required Namespace
Till Now Project Setup Is Ready....
.Netcore Api + angular 6 + EFCore Project Part - 2
No comments:
Post a Comment