簡體   English   中英

代碼優先實體框架添加對象

[英]Code First Entity Framework Adding Object

我正在使用Code First Entity Framework,並且卡在需要向一個對象添加功能列表的地方。

例如:

我有以下聚合。

1)Plan.cs

2)Feature.cs

現在,我可以使用Plan Agg創建一個計划,並將那些功能分配給該計划。

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

計划=基本套餐,高級套餐

功能=上傳,刪除,創建,更新。

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

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

任何幫助都感激不盡。

編輯:更新的代碼:

Agg:

   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:

我只是注意到在“功能”的數據庫表中,我有一個外鍵“ Plan_PlanId”

現在,當我將功能分配給基本軟件包時:功能表將更新如下:

FeatureId FeatureName Plan_PlanId

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

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

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

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

FeatureId FeatureName Plan_PlanId

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

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

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

但我想在兩個計划中都提供這些功能。

請幫助大家。

在這種情況下,我想您將在計划中擁有一系列功能,如下所示

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

在這種情況下,我想知道您是否在第一個計划中將功能設置為null,然后更新新計划。 在上面的示例中,將Basic計划的PlanFeatures設置為null,然后將它們添加到Premium包中。 如果您可以向我們展示代碼,那么人們可以更快地為您提供幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM