参数类型 'Edm.String' 和 'Edm.Int32' 与此操作不兼容
The argument types 'Edm.String' and 'Edm.Int32' are incompatible for this operation
我收到类似上面标记的错误,该错误将位于
的位置
return View(st.employees.Find(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
方法时,通常会发生此异常。
从Find
方法的official documentation,可能会抛出下面的异常
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.
确保调用 Find
方法时使用相同类型的变量。
在您的代码中,您将整数变量传递给 Find
方法。从错误我相信你的实体 类 主键不是 int 类型。可能是 Guid
类型,在这种情况下,请确保将有效的 Guid 值传递给 Find
方法。
您可以打开 edmx 文件并查看您的密钥类型,并确保将相同类型传递给 Find
方法。
只需右键单击您的 edmx 文件中的实体和 select 属性。
您似乎在遵循 MVC 模式。
我也遇到了这个错误,这是因为我将 id 参数作为整数传递,而不是我在模型中声明的 "string"。
样本:
public class Object
{
public string id { get; set; }
public string property1{ get; set; }
public string property2{ get; set; }
public string property3{ get; set; }
}
Find()
将只接受数据类型类似于 table 的主键的参数,您将为其使用 Find()
.
我收到类似上面标记的错误,该错误将位于
的位置return View(st.employees.Find(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
方法时,通常会发生此异常。
从Find
方法的official documentation,可能会抛出下面的异常
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.
确保调用 Find
方法时使用相同类型的变量。
在您的代码中,您将整数变量传递给 Find
方法。从错误我相信你的实体 类 主键不是 int 类型。可能是 Guid
类型,在这种情况下,请确保将有效的 Guid 值传递给 Find
方法。
您可以打开 edmx 文件并查看您的密钥类型,并确保将相同类型传递给 Find
方法。
只需右键单击您的 edmx 文件中的实体和 select 属性。
您似乎在遵循 MVC 模式。
我也遇到了这个错误,这是因为我将 id 参数作为整数传递,而不是我在模型中声明的 "string"。
样本:
public class Object
{
public string id { get; set; }
public string property1{ get; set; }
public string property2{ get; set; }
public string property3{ get; set; }
}
Find()
将只接受数据类型类似于 table 的主键的参数,您将为其使用 Find()
.