C# 代码优先实体框架添加对象
我正在使用代码优先实体框架,我在一个需要向一个对象添加特性列表的地方遇到了麻烦 例如: 我有以下几点 1) Plan.cs 2) Feature.cs 现在,我可以使用plan Agg创建一个计划,并将这些特征分配给该计划C# 代码优先实体框架添加对象,c#,entity-framework,ef-code-first,entity,code-first,C#,Entity Framework,Ef Code First,Entity,Code First,我正在使用代码优先实体框架,我在一个需要向一个对象添加特性列表的地方遇到了麻烦 例如: 我有以下几点 1) Plan.cs 2) Feature.cs 现在,我可以使用plan Agg创建一个计划,并将这些特征分配给该计划 // ids of features to be assigned // var FeatureIds = List<int>{1,2,3}; // Get the Plan with th
// 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();
}
但是,当我创建新平面并指定相同的要素时,第一个平面中的要素将被删除并指定给第二个平面
计划=基本套餐、高级套餐
功能=上传、删除、创建、更新
现在,如果我将所有功能分配给基本包,即上载、删除、创建和更新。它工作得很好,但是当我将删除和创建分配给高级包时。。它被分配给高级包,但删除和创建的包将从基本包中删除
对不起,我无法正确地解释我的情况
任何帮助都将不胜感激
编辑:更新代码:
Agg:
公共类计划:实体
{
//其他属性。。。
公共虚拟列表{get;set;}
//其他属性都在这里。。。
}
..为平面图分配特征的功能
// 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();
}
//要分配的要素ID
//var-FeatureIds=List{1,2,3};
//获取具有计划Id的计划。
var plan=\u planRepository.Get(planId);
//获取具有服务Id的服务。
服务服务=_serviceRepository.Get(serviceId);
//获取传递的FeatureId的功能。
var features=service.features.FindAll(x=>FeatureIds.Contains(x.FeatureId));
//我们将开始订阅过程的事务处理
使用(var transactionScope=new transactionScope())
{
//将列表添加到计划中。
Plan.Features.AddRange(特性);
//保存更改
_planRepository.UnitOfWork.Commit();
transactionScope.Complete();
}
编辑2:
我刚刚注意到,在“Features”的数据库表中,我有一个ForeignKey“Plan\u PlanId”
现在,当我将功能分配给基本包时:功能表更新如下:
FeatureId FeatureName平面图\u平面图
1-------创造--------1
2------删除------1
3----更新----1
现在,当我将这些功能分配给Premium软件包时:我只将2个功能分配给Premium软件包
FeatureId FeatureName平面图\u平面图
1-------创造--------1
2------删除------2
3----更新----2
但我希望在两个计划中都能使用这些功能
请帮助大家。在这种情况下,我猜您将在计划中拥有一系列功能,如下图所示
public class Plan
{
public List<Features> PlanFeatures {get;set;}
// other properties go here...
}
公共课程计划
{
公共列表特征{get;set;}
//其他属性都在这里。。。
}
在这种情况下,我想知道您是否在第一个计划中将特性设置为null,然后更新新计划。在上面的示例中,将
Basic
计划的PlanFeatures
设置为null,然后将它们添加到Premium
包中。如果您能向我们展示代码,人们可以帮助您更快一点。事实上,我的操作与您描述的属性完全相同,但运气不好。@SyedabDulkader:请不要将PlanFeatures
设置为null,因为它将通过删除计划的功能在那里触发delete语句。请检查上面的代码。我还将虚拟关键字与PlanFeatures一起使用。在使用此方法之前,您是否检查了basic
计划的更新位置。因为当您调用savechanges()
时,所有对象都将被持久化。请检查代码。不,在这个方法之前没有调用任何东西,它是直接从控制器调用的。最好发布一些代码示例。@Dennis:我已经添加了代码朋友。