简体   繁体   中英

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.

example :

i have the following Aggregates.

1) Plan.cs

2) Feature.cs

now i can create a plan with the Plan Agg and assign those Features to the plan.

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. 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 :

   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.

            // 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 :

i just noticed that in the Database Table for "Features" i have the ForeignKey "Plan_PlanId"

now when i assign the Features to the Basic Package : the features table gets updated as follows :

FeatureId FeatureName Plan_PlanId

1 -------- Create -------- 1

2 -------- Delete -------- 1

3 -------- Update -------- 1

now then when i assign these features to the Premium Package : i am assigning only 2 features to premium package.

FeatureId FeatureName Plan_PlanId

1 -------- Create -------- 1

2 -------- Delete -------- 2

3 -------- Update -------- 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. In the above sample it will be setting the PlanFeatures to null for Basic plan and then adding them to the Premium package. If you can show us the code, people can help you a bit faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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