Database Queries in MVC Model

后端 未结 2 964
不思量自难忘°
不思量自难忘° 2021-02-09 13:43

In a MVC project if I put LINQ queries in Model, is it against the MVC Pattern?

namespace DocLibrary.Models
{
    public class Auth         


        
2条回答
  •  粉色の甜心
    2021-02-09 14:17

    In a MVC project if I put LINQ queries in Model, is it against the MVC Pattern?

    No, it's not against the MVC pattern. Database queries are perfectly fine in the Model. Obviously a clear distinction should be made between the Model and the View Model that you are passing to your views. The view model should not contain any database specific stuff.

    Or should I move these methods (GetNameById, GetAuthorById) to Controller?

    Absolutely not. The controller's responsibility is not to query a database. A controller responsibility is to talk to the Model, build a view model and pass this view model to the view. A controller shouldn't even know what a database is.

提交回复
热议问题