在MVC項目中,如果我在Model
中插入LINQ查詢,是否違反了MVC模式?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
)來控制?