1

これは過去 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 クラスから継承します。

4

2 に答える 2

0

問題の解決策を見つけました。私の他の同僚の 1 人が、データベース内の 1 つのフィールド (propertyid ではなく、別の無関係なフィールド) のデータ型を変更しましたが、プログラム内では変更しませんでした。私は自分で変更を完全に忘れました。私は今、ばかげていると感じています。

于 2013-08-27T19:56:30.413 に答える