2016-02-22 24 views
0

我有一個.aspx頁面,我想上傳一個Excel文件並將excel文件的數據插入到數據庫中。實體類型不是當前上下文模型的一部分。當從aspx頁面上傳Excel文件

我對這種做法沒有任何意見,所以在谷歌搜索,並找到了一個教程,如何做到這一點。然後創建一個單獨的項目,遵循教程中給出的相同指令並完成。然後在我的實際項目中實現相同的東西。

要執行我的任務,我正在使用EntityFramework。現在,所有的事情被設置在它的地方,但是當我運行我的項目,並上傳excel文件,並按下提交按鈕,它給我的錯誤The entity type tblUserDetails is not part of the model for the current context.

這裏是我的代碼:

// Import to Database 
      using (pwpEntities dc = new pwpEntities()) 
      { 
       foreach (DataRow dr in ds.Tables[0].Rows) 
       { 
        string FirstName = dr["First Name"].ToString(); 
        var v = dc.tblUserDetailsProp.Where(a => a.vchFirstName.Equals(FirstName)).FirstOrDefault(); 
        if (v != null) 
        { 
         // Update here 
         v.CompanyName = dr["Company Name"].ToString(); 
         v.vchLocation = dr["Location"].ToString(); 
         v.vchFirstName = dr["First Name"].ToString(); 
         v.vchMobile1 = dr["Mobile 1"].ToString(); 
         v.vchMobile = dr["Mobile"].ToString(); 
         v.vchEmail = dr["Email"].ToString(); 
         v.vchUserName = dr["User Name"].ToString(); 
         v.ServiceId = Convert.ToInt32(dr["Service Id"]); 
         v.vchPassword = dr["Password"].ToString(); 
         v.UserTypeFkId = Convert.ToInt32(dr["UserTypeFkId"]); 
        } 
        else 
        { 
         // Insert 
         dc.tblUserDetailsProp.Add(new tblUserDetails 
         { 
          CompanyName = dr["Company Name"].ToString(), 
          vchLocation = dr["Location"].ToString(), 
          vchFirstName = dr["First Name"].ToString(), 
          vchMobile1 = dr["Mobile 1"].ToString(), 
          vchMobile = dr["Mobile"].ToString(), 
          vchEmail = dr["Email"].ToString(), 
          vchUserName = dr["User Name"].ToString(), 
          ServiceId = Convert.ToInt32(dr["Service Id"]), 
          vchPassword = dr["Password"].ToString(), 
          UserTypeFkId = Convert.ToInt32(dr["UserTypeFkId"]) 
         }); 
        } 
       } 

       dc.SaveChanges(); 
      } 

它拋出異常這裏var v = dc.tblUserDetailsProp.Where(a => a.vchFirstName.Equals(FirstName)).FirstOrDefault();

其他網頁代碼: MyModel.cs: -

public partial class tblUserDetails 
{ 
    public int intId { get; set; } 
    public string vchFirstName { get; set; } 
    public string vchLastName { get; set; } 
    public string vchUserName { get; set; } 
    public string vchPassword { get; set; } 
    public string vchEmail { get; set; } 
    public string vchIMEI { get; set; } 
    public Nullable<double> Weight { get; set; } 
    ... 
} 

MyModel.Context.cs: -

using System; 
using System.Data.Entity; 
using System.Data.Entity.Infrastructure; 

public partial class pwpEntities : DbContext 
{ 
public pwpEntities() 
    : base("name=pwpEntities") 
{ 
} 

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    throw new UnintentionalCodeFirstException(); 
} 

public DbSet<tblUserDetails> tblUserDetailsProp { get; set; } 
} 

請幫助我,我已經失去了我大量的時間消除這種錯誤。 我訪問了互聯網上的所有網站,但沒有找到解決方案。

+0

http://stackoverflow.com/questions/20688922/the-entity-type-type-is-not-part-of-the-model-for-the-current-context,http://stackoverflow.com/questions/22394603 /越來越多的實體類型模型不是該模型的最新當前狀態,http://stackoverflow.com/questions/13634819/entityset-system -invalidoperationexception-the-entity-type-is-not-part-of-the等等。看起來好像有很多解決方案可以通過Google搜索錯誤信息。他們都不爲你工作嗎? – David

+0

我試圖改變連接字符串與默認相同。試圖驗證.edmx並嘗試了其他一些技巧,但都沒有工作。 還有一點是我非常喜歡這個概念,所以不能說我已經正確地實施了它們。但我正在盡我所能。所以我會很高興,如果你看看代碼並幫助我。 –

回答

-1

如果在您的項目中發生此錯誤,可能有幾個原因。仔細檢查並確保實體映射是正確的。進一步請參閱此Post

+0

如何檢查實體映射?正如我所看到的,當我放置檢查點時一切正常,但是當涉及到這一行時:「var v = dc.tblUserDetailsProp.Where(a => a.vchFirstName.Equals(FirstName))。FirstOrDefault();」它會給出錯誤。 –

+0

你是從數據庫生成模型? –

+0

最有可能您的模型(POCO或edmx)不公開/映射此類型的DbSet。這可能是因爲你的派生類型是部分的。 – DevilSuichiro

相關問題