1

我使用Asp.Net樣板框架,我通過Code-First方法在數據庫中創建了一個表。現在我想從該表中獲取記錄,但應用這樣的查詢返回空數據。爲什麼?Asp.Net Boilerplate,如何在數據庫表上應用查詢?

MyAppDbContext db = new MyAppDbContext(); 
var list = db.NewTables.ToList(); 

但是List返回爲空,而表實際上包含數據。

+0

是'MyAppDbContext'包含3寧正確的ConnectionString? – meorfi

+0

是的。你知道樣板嗎? – MMG

+0

@MMG這解決了嗎? – aaron

回答

1

進樣IRepository,而不是直接使用DbContext

public class TaskManager : DomainService, ITaskManager 
{ 
    private readonly IRepository<Task, long> _taskRepository; 

    public TaskManager(IRepository<Task, long> taskRepository) 
    { 
     _taskRepository = taskRepository; 
    } 

    public void GetTasks() 
    { 
     var tasks = _taskRepository.GetAll().ToList(); 
    } 
} 

閱讀ABP文檔更多: