繁体   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