これは過去 1 時間私を悩ませてきましたが、正確に何が問題なのかわかりません。他のいくつかのオブジェクト (正常に返される) と共に、データベースからオブジェクトを取得しようとしています。例外が発生します
object of type 'system.string' cannot be converted to type 'system.int32'
ここでは変数が 2 つしかなく、どちらもデータ型が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 クラスから継承します。