我正在使用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
但我想有这些功能中都计划上市。
请帮助球员。
发布一些代码示例要好得多。 – Dennis 2013-05-08 06:22:54
@丹尼斯:我已经添加了密码的朋友。 – 2013-05-08 06:34:26