Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 958
Manoj Kumar Miriyala Avatar asked Aug 08 '16 15:08

Manoj Kumar Miriyala


2 Answers

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.

enter image description here

like image 156
Shyju Avatar answered Sep 28 '22 17:09

Shyju


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; }
}
like image 42
jcdevilleres Avatar answered Sep 28 '22 17:09

jcdevilleres