2013-05-08 65 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

相关问题