2013-05-08 99 views
0

我正在使用Code First Entity Framework,我被困在一個地方,我需要爲一個對象添加一個功能列表。代碼第一個實體框架添加對象

示例:

我有以下聚集。

1)Plan.cs

2)Feature.cs

現在我可以創建一個計劃AGG的計劃,並分配這些功能的計劃。

但是當我創建一個新的計劃並分配相同的功能時,第一個計劃中的功能將被刪除並分配給第二個計劃。

計劃=基本包,高級套裝

特點=上傳,刪除,創建,更新。

現在,如果我將所有功能分配給基本軟件包,即上傳,刪除,創建和更新。它工作正常,但是當我將Delete和Create分配給高級包時,它被分配到高級包,但這些刪除和創建從基本包中刪除。

對不起,因爲我無法正確解釋我的情況。

任何幫助將不勝感激。

編輯:更新的代碼:

總比分執行規劃的功能分配

public class Plan:Entity 
    { 
    // other properties ... 
    public virtual List<Features> PlanFeatures {get;set;} 
    // other properties go here... 
    } 

..function。

  // ids of features to be assigned 
      // var FeatureIds = List<int>{1,2,3}; 

      // Get the Plan with the Plan Id. 
      var plan = _planRepository.Get(planId); 


      // Get the Service with Service Id. 
      Service service = _serviceRepository.Get(serviceId); 

      // Get the Features for the passed FeatureIds. 
      var features = service.Features.FindAll(x => FeatureIds.Contains(x.FeatureId)); 

      //We will begin a transaction for the subscription process 
      using (var transactionScope = new TransactionScope()) 
      { 

       // Add the List<Feature> to Plan. 
       Plan.Features.AddRange(features); 

       //Save changes 
       _planRepository.UnitOfWork.Commit(); 
       transactionScope.Complete(); 
      } 

編輯2:

我只注意到在數據庫表「功能」我有ForeignKey的「Plan_PlanId」

現在

當我指派的功能基本套餐:功能如下表得到更新:

FEATUREID FeatureName Plan_PlanId

1 -------- --------創建1

2 -------- --------刪除1

3 -------- --------更新1

現在,當我將這些功能分配給高級套餐時:我只將2個功能分配給高級套餐。

FEATUREID FeatureName Plan_PlanId

1 -------- --------創建1

2 --------刪除------ - 2

3 -------- --------更新2

但我想有這些功能中都計劃上市。

請幫助球員。

+0

發佈一些代碼示例要好得多。 – Dennis 2013-05-08 06:22:54

+0

@丹尼斯:我已經添加了密碼的朋友。 – 2013-05-08 06:34:26

回答

0

在這種情況下,我想你會具有像下面

public class Plan 
{ 
    public List<Features> PlanFeatures {get;set;} 
    // other properties go here... 
} 

所示。在這種情況下,一個計劃的功能集合,我想知道你是否已設定功能爲null在第一個計劃中,然後更新新計劃。在上面的示例中,它將設置爲null,用於Basic計劃,然後將它們添加到Premium包。如果你能向我們展示代碼,人們可以更快地幫助你。

+0

的確如我所描述的那樣完全像你所描述的那樣,但沒有運氣。 – 2013-05-08 06:19:22

+0

@SyedAbdulQadeer:請不要將PlanFeatures設置爲null,因爲它會通過刪除計劃的功能來觸發刪除語句。 – Saravanan 2013-05-08 06:21:59

+0

請檢查上面的代碼。我也使用VIRTUAL關鍵字與PlanFeatures – 2013-05-08 06:27:41

相關問題