2016-08-08 179 views
2

我越來越像上面標籤的錯誤,這將是在的參數類型「Edm.String」和「Edm.Int32」是不兼容此操作

返回查看(st.employees的地方。查找(ID));

以上地方而已,任何一個可以幫助我從這裏!和我的代碼是

 namespace StartApp.Controllers 
    { 
public class EmployController : Controller 
{ 
    StartEntities st = new StartEntities(); 
    //List 
    public ActionResult List() 
    { 
     return View(st.employees.ToList()); 
    } 
    //Details 
    public ActionResult Details(int id = 0) 
    { 
     return View(st.employees.Find(id)); 
    } 
    //Create 
    public ActionResult Create() 
    { 
     return View(); 
    } 


    [HttpPost,ValidateAntiForgeryToken] 
    public ActionResult Create(employee e) 
    { 
     using(st) 
     { 
      st.employees.Add(e); 
      try 
      { 
       st.SaveChanges(); 
      } 
      catch 
      { 
       System.Diagnostics.Debug.WriteLine("Here is an error"); 
      } 
     } 
     return RedirectToAction("List"); 
    } 
    //edit 
    public ActionResult Edit(int id = 0) 
    { 

      return View(st.employees.Find(id)); 

    } 

    [HttpPost,ValidateAntiForgeryToken] 
    public ActionResult Edit(employee e) 
    { 
     st.Entry(e).State = EntityState.Modified; 
     st.SaveChanges(); 
     return RedirectToAction("List"); 
    } 
    //Delete 
    public ActionResult Delete(int id = 0) 
    { 
     return View(st.employees.Find(id)); 
    } 
    [HttpPost,ActionName("Delete")] 
    public ActionResult Delete_conf(int id) 
    { 
     employee emp = st.employees.Find(id); 
      st.employees.Remove(emp); 
      st.SaveChanges(); 
      return RedirectToAction("List"); 
    } 

} 

}

任何一個可以幫助我改正這個錯誤!當你的實體主鍵是A型的,你是傳遞一個變量,它是A型的不給Find方法

+0

看看'Employee'實體。 「Key」的類型是什麼? –

+0

它只是主鍵 –

+0

班級中的密鑰的數據類型是什麼。 (查看edmx中的字段屬性或代碼文件) –

回答

2

此異常通常發生。

official documentationFind的方法,它可能會引發以下異常

出現InvalidOperationException

如果類型的鍵值不匹配的類型爲的 鍵值拋出要找到的實體類型。

確保在調用Find方法時使用相同的類型變量。

在代碼中,要傳遞一個整型變量的Find方法。從錯誤我相信你的實體類主鍵不是int類型。可能是Guid類型,在這種情況下,請確保您將有效的Guid值傳遞給Find方法。

您可以打開EDMX文件,看看你的密鑰類型,並確保你傳遞同類型的Find方法。

只需右鍵單擊edmx文件中的實體並選擇屬性。

enter image description here

+0

感謝您的建議,我是mvc的新手。我可以知道什麼是A型。 –

+0

它可以是任何類型(例如:Guid)。在你的代碼中,你正在傳遞一個int變量。檢查你的實體類在EDMX文件,看看它是什麼類型 – Shyju

+0

是的,我不是說它爲int。我宣佈爲varchar(50)。現在我將通過什麼值而不是0。 –

1

好像您遵循MVC模式。

我得到這個錯誤,以及那是因爲我是路過的ID參數作爲我在模型中聲明的一個整數,而不是「串」。

樣品:

public class Object 
{ 
    public string id { get; set; } 
    public string property1{ get; set; } 
    public string property2{ get; set; } 
    public string property3{ get; set; } 
} 
相關問題