An easy way to handle this is with the repository template. This is not the best way to do this. But you will give you an idea of how you can handle this repository template.
create a repository to execute all db transactions
public interface IRepository { Order GetOrder(int orderId); } public class Repository : IRepository { YourDBContext db; public Repository() { db = new YourDBContext (); } public User GetOrder(int orderId) { return db.Orders.FirstOrDefault(s=>s.OrderID==orderId); } }
You can create this in the same project (in the "Data Access Logic" section) or create a separate class library for it (and link to it wherever you use it).
And now in your controller, after importing the necessary namespaces, just create an object of your repository and call the method that interests you
public OrderController :Controller { protected IRepository repo; public OrderController() { repo=new Repository(); } public OrderController(IRepository repositary) {
You might consider using a presentation model if you think your presentation needs it. in this case, you need to read the property values from your domain object and install an instance of your view model and return it to your view.
You can move the code to different classes / layers / projects as your code / functionality grows.
Shyju
source share