简体   繁体   English

代码优先实体框架添加对象

[英]Code First Entity Framework Adding Object

i am working with Code First Entity Framework and i am stucked at a place where i need to add a list of feature's to one object. 我正在使用Code First Entity Framework,并且卡在需要向一个对象添加功能列表的地方。

example : 例如:

i have the following Aggregates. 我有以下聚合。

1) Plan.cs 1)Plan.cs

2) Feature.cs 2)Feature.cs

now i can create a plan with the Plan Agg and assign those Features to the plan. 现在,我可以使用Plan Agg创建一个计划,并将那些功能分配给该计划。

but when i create a new plan and assign the same features then the features from the first plan are removed and assigned to second plan. 但是,当我创建一个新计划并分配相同的功能时,第一个计划中的功能将被删除并分配给第二个计划。

Plan = Basic Package , Premium Package 计划=基本套餐,高级套餐

Features = Upload , Delete , Create , Update. 功能=上传,删除,创建,更新。

now if i assigned all the features to the Basic Package ie, Upload , Delete , Create and Update. 现在,如果我将所有功能分配给基本软件包,即Upload,Delete,Create和Update。 it works fine , but when i assign the Delete and Create to the Premium package .. it gets assigned to the Premium Package but those Delete and Create are removed from the Basic Package. 它工作正常,但是当我将删除和创建分配给高级软件包时..将其分配给高级软件包,但将这些删除和创建从基本软件包中删除。

sorry , as i am unable to explain my situation correctly. 对不起,因为我无法正确解释我的情况。

any help will be much appreciated. 任何帮助都感激不尽。

EDIT : Updated Code : 编辑:更新的代码:

Agg : Agg:

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

..function that perform the assignment of features to the plan. 将功能分配给计划的..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();
            }

EDIT 2 : 编辑2:

i just noticed that in the Database Table for "Features" i have the ForeignKey "Plan_PlanId" 我只是注意到在“功能”的数据库表中,我有一个外键“ Plan_PlanId”

now when i assign the Features to the Basic Package : the features table gets updated as follows : 现在,当我将功能分配给基本软件包时:功能表将更新如下:

FeatureId FeatureName Plan_PlanId FeatureId FeatureName Plan_PlanId

1 -------- Create -------- 1 1 --------创建-------- 1

2 -------- Delete -------- 1 2 --------删除-------- 1

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

now then when i assign these features to the Premium Package : i am assigning only 2 features to premium package. 现在,当我将这些功能分配给高级套餐时:我仅将2个功能分配给高级套餐。

FeatureId FeatureName Plan_PlanId FeatureId FeatureName Plan_PlanId

1 -------- Create -------- 1 1 --------创建-------- 1

2 -------- Delete -------- 2 2 --------删除-------- 2

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

But i want to have these feature to be available in both Plans. 但我想在两个计划中都提供这些功能。

please help guys. 请帮助大家。

In this case, I guess you will be having a collection of features in the plan like the one shown below 在这种情况下,我想您将在计划中拥有一系列功能,如下所示

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

In this case, i would like to know whether you have set the features to null in the first plan and then updating the new plan. 在这种情况下,我想知道您是否在第一个计划中将功能设置为null,然后更新新计划。 In the above sample it will be setting the PlanFeatures to null for Basic plan and then adding them to the Premium package. 在上面的示例中,将Basic计划的PlanFeatures设置为null,然后将它们添加到Premium包中。 If you can show us the code, people can help you a bit faster. 如果您可以向我们展示代码,那么人们可以更快地为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM