2012-02-24 12 views
0

VS2010を使用しているWebアプリケーションプロジェクトでORMとしてLINQを使用しています。 SQLデータベースにERを定義し、ORMにデータベーステーブルをドラッグアンドドロップしました。転送テーブルには、4つの異なる列を持つ病院テーブルとの4つの関係があります。列の1つがFK、int、nullではありません。他の3つはFK、int、nullです。 DBMLでは、TransferクラスにはHospitalクラスへの4つの関連があります。必要なTransfer.Hospital(FK、int、not null)との関連付けには、次のプロパティがあります。ASP.Net MVC2のアソシエーション

Cardinality: OneToMany 
Child Property: True 
    Access: Public 
    Inheritance Modifier: (None) 
    Name = Transfers1 
Parent Property 
    Access: Public 
    Inheritance Modifier: (None) 
    Name: HospitalSrcOrDest 
Participating Properties: Hospital.HospitalID -> Transfer.Hospital 
Unique: False 

ビューページで使用されるビューモデルには、転送タイプの転送プロパティがあります。 Model.Transfer.Hospitalは常に整数値を持ちます。ただし、次のページスクリプトのModel.Transfer.HospitalSrcOrDestは常にnullです。 HospitalNameはHospital_Transfer1の関連付けによって自動的に取得されませんか?ありがとう。

<%: Model.Transfer.HospitalSrcOrDest != null ? Model.Transfer.HospitalSrcOrDest.HospitalName : string.Empty%> 

答えて

0

私は私のリポジトリのコンストラクタを変更し、それはどんな違いがありません。

public AppRepository() //constructor 
{ 
     // Create DataLoadOptions 
     DataLoadOptions dlo = new DataLoadOptions(); 

     // Always fetch source or destiation hospital when we get transfer 
     dlo.LoadWith<Transfer>(t => t.HospitalSrcOrDest); 

     // Set these options on the DataContext 
     db.LoadOptions = dlo; 
.... 

} 

私の理解では、LINQは、デフォルト(http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/10/06/linq-to-sql-deferred-loading-lazy-load.aspx)として積極的なロードを使用しています。私のView Modelでは、TransferはControllerによって渡されます。 View ModelにHospitalSrcOrDestプロパティを追加し、関連付けをナビゲートして値をロードする必要があります。

public ActionResult Edit(string id) 
{ 
.... 

Transfer transfer = base.ApplicationRepository.GetTransfer(intID); 

if (transfer == null) 
    { 
    ViewData["Message"] = "There is no transfer record"; 
    return View("NotFound"); 
    } 

TransferViewModel vm = new TransferViewModel(base.ApplicationRepository, transfer, "Edit"); 

return View(vm); 
} 
0

怠惰な負荷の問題である可能性があります。病院がビューにアクセスする前にデータコンテキストを置き換えていますか?デフォルトでは、L2Sは遅延関係を読み込みます。つまり、プロパティが実際にアクセスされるまで、データベースから取得されません。ビューが病院にアクセスするまでに接続が切れた場合、それを取得できないことがあります。ちょうど推測。

負荷/選択を実行した後、コントローラでテストするには、すぐに病院にアクセスして強制的に負荷をかけてください。

あなたはいつも病院を熱心に取得したい場合は、ここにあなたが何をすべきかを示してリンクです: http://blog.dantup.com/2009/04/eager-fetching-of-relationships-with.html

+0

loadoptionをリポジトリコンストラクタに提案どおりに設定しましたが、違いはありません。 // DataLoadOptionsを作成します。 DataLoadOptions dlo = new DataLoadOptions(); //投稿を取得したときに必ずタグを取得します。 dlo.LoadWith (p => p.Tags); //これらのオプションをDataContextに設定します。 db.LoadOptions = dlo; – user266909

関連する問題