2016-07-29 160 views
-1

在我的數據庫中,實體框架上下文正在使用我有一個視圖。 vwReportMonths。它返回的是一組與我需要報告的表格中記錄的月份開始相對應的日期。該視圖正常工作並返回正確的日期。實體框架無法找到數據庫視圖

我創建了模型

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Cards 
{ 
    public class ReportMonthView 
    { 
     public DateTime CardMonth { get; set; } 
    } 
} 

我創建了一個映射類:

using Card; 
using System; 
using System.Collections.Generic; 
using System.Data.Entity.ModelConfiguration; 
using System.Linq; 
using System.Text; 

namespace DAL.Card.Mapping 
{ 
    class ReportMonth_Mapping : EntityTypeConfiguration<ReportMonthView> 
    { 
     public ReportMonth_Mapping() 
     { 
      ToTable("dbo.vwReportMonths"); 

      HasKey(x => x.CardMonth); 
     } 
    } 
} 

我添加映射到我的OnModelCreating方法在我使用的上下文。

modelBuilder.Configurations.Add(new TrendReportMonth_Mapping()); 

而且我也在上下文中創建了DbSet。

public DbSet<ReportMonthView> ReportMonths { get; set; } 

但是,當我試圖從我的控制器訪問接入角度,它拋出一個無效的對象名稱dbo.vwReportMonthView「

public JsonResult GetReportMonths() { 

     var data = _safetyContext.TrendReportMonths; 
     return Json(data, JsonRequestBehavior.AllowGet); 
    } 

我缺少什麼?我以爲我昨天對EF有了一個頓悟,但顯然不是。

再次感謝您的所有幫助。

+0

你試過ToTable(「vwReportMonths」); ? –

+0

是的,我也嘗試了ToTable(「CardDB.dbo.vwReportMonths」)和ToTable(「CardDB.vwReportMonths」),結果相同。 –

+0

所有其他DbSets的工作和檢查配置設置? –

回答

0

我終於找到答案。之前的開發人員給了我不好的信息。我在錯誤的數據庫中提出了所有的觀點。一旦我再次檢查連接字符串,我看到了我的錯誤。不再聽高級開發人員的指示。我將首先檢查。