我正在使用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