在 MVC 项目中,如果我将LINQ查询放入Model
,是否违反 MVC 模式?
namespace DocLibrary.Models
{
public class Author
{
private DocLibraryContext db = new DocLibraryContext();
[Key]
public Int32 AuthorId { get; set; }
[StringLength(20)]
public String Name { get; set; }
..
public string GetNameById(int AuthorId)
{
var query = from a in db.Author
where a.AuthorId == AuthorId
select a.Name;
return query.FirstOrDefault();
}
public Author GetAuthorById(int AuthorId)
{
var query = from a in db.Author
where a.AuthorId.Equals(AuthorId)
select a;
return query.FirstOrDefault();
}
}
或者我应该将这些方法 ( GetNameById
, GetAuthorById
) 移到 Controller 中吗?