在过去的一个小时里,这一直困扰着我,我无法找出到底出了什么问题。我正在尝试从我们的数据库中检索一个对象以及其他几个对象(返回正常)。我得到一个例外
object of type 'system.string' cannot be converted to type 'system.int32'
这很奇怪,因为这里只有两个变量在起作用,而且都是数据类型int。这是代码。
控制器
public ActionResult EditGABuyContract(int id)
{
var p = PropertyService.GetPropertyBy(id);
if (p == null)
return RedirectToRoute(new { Controller = "Dashboard", Action = "Index" });
var purchase = PropertyService.GetPropertyPurchaseBy(id); //This works just fine
var buyContract = PropertyService.GetGABuyContractById(id); //This leads to the exception
...
服务
public PropertyGABuyContract GetGABuyContractById(int propertyid) //This leads to an exception
{
try
{
return ((IRepositoryBase)PropertyRepository).GetByPropertyId<PropertyGABuyContract>(propertyid);
}
catch (Exception ex)
{
Log.Error(ex);
return null;
}
}
public PropertyPurchase GetPropertyPurchaseBy(int propertyid) //This does NOT lead to an exception
{
try
{
return ((IRepositoryBase) PropertyRepository).GetByPropertyId<PropertyPurchase>(propertyid);
}
catch (Exception ex)
{
Log.Error(ex);
return null;
}
}
存储库
public T GetByPropertyId<T>(int id) where T : PropertyBase, new()
{
return repo.Single<T>(t => t.PropertyId == id); //Exception occurs here for only GetGABuyContract() method and not for the GetPropertyPurchaseById() method
}
这个例外对我来说没有意义。对象 PropertyPurchase 和 PropertyGABuyContract 都从派生 PropertyId 的同一 PropertyBase 类继承。