The argument types 'Edm.String' and 'Edm.Int32' are incompatible for this operation

百般思念 提交于 2019-12-23 18:24:06

问题


I am getting the error like above tag which will be at the place of

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

above place only ,can any one help me from this! and my code is

     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");
    }

}

}

can any one help me to rectify that error!


回答1:


This exception usually happens when your entities primary key is of type A and you are passing a variable which is not of type A to the Find method.

From the official documentation of Find method, It may throw the below exception

InvalidOperationException

Thrown if the types of the key values do not match the types of the key values for the entity type to be found.

Make sure you use the same type variable when you call the Find method.

In your code, you are passing an integer variable to the Find method. From the error i believe your entity classes primary key is not int type. May be it is Guid type, in that case, make sure you are passing a valid Guid value to the Find method.

You can open up the edmx file and see the type of your key and make sure you pass the same type to the Find method.

Just right click on the entity in your edmx file and select properties.




回答2:


it seems like you are following the MVC pattern.

I got this error as well and it is because I was passing the id parameter as an integer instead of "string" as I declared in my model.

Sample:

public class Object
{
    public string id { get; set; }
    public string property1{ get; set; }
    public string property2{ get; set; }
    public string property3{ get; set; }
}



回答3:


Find() will only accept parameter with datatype similar to primary key of table for which you are going to use Find().



来源:https://stackoverflow.com/questions/38832906/the-argument-types-edm-string-and-edm-int32-are-incompatible-for-this-operat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!