2012-02-09 46 views
0

我有一個小問題,它給了我一個StackOweflow問題。具有自引用實體的AutoMapper地圖實體

我將EF 4.1與複雜類型結合使用,問題是您需要創建複雜類型的實例,即使所有值都爲空,以使其與EF一起工作。

所以現在我有一個類,它看起來像這樣..

public class GoodsItem{ 

    public GoodsItem InnerGoodsItem{get;set;} 

    //-- A lot of other properties needed for this class 

    public GoodsItem() 
    { 
    this.InnerGoodsItem = new GoodsItem(); 
    } 
} 

我需要在構造函數中的代碼,EF爲它應該工作,但後來我每次創建GoodsItem它會創建一個新的如何創建新的GoodsItem等...

如何使用AutoMapper解決此問題並保持EF 4.1的快樂。

先進的感謝...

回答

0

無法看到該構造函數如何工作。也許你可以改變你的類屬性的get初始化內的項目,如:

public class GoodsItem{ 

    private GoodsItem _innerGoodsItem; 
    public GoodsItem InnerGoodsItem 
    { 
     get 
     { 
      if (_innerGoodsItem == null) _innerGoodsItem = new GoodsItem(); 
      return _innerGoodsItem; 
     } 
     set { _innerGoodsItem = value; } 
    } 

    //-- A lot of other properties needed for this class 

    public GoodsItem() 
    { 
     //No longer need this call in ctor 
     //this.InnerGoodsItem = new GoodsItem(); 
    } 
} 

不知道這是否會導致與EF一個問題,但(謝天謝地,我已經差不多避免EF爲止! )。

+0

感謝您的建議。我採取了簡單的方法,使InnerGoods成爲GoodsItem的集合。問題解決了。而那種解決方案實際上更正確的時候,我們已經讓商人感到震驚...... – 2012-02-15 13:49:12

+0

好吧,只要生意興隆!隨時upvote ;-) – Simon 2012-02-15 15:04:31