C# 更新实体框架中现有的EntityCollection
我尝试使用链接到实体,并且我希望在我的应用程序中直接使用我的实体C# 更新实体框架中现有的EntityCollection,c#,entity-framework,linq-to-entities,entity-framework-4,edmx,C#,Entity Framework,Linq To Entities,Entity Framework 4,Edmx,我尝试使用链接到实体,并且我希望在我的应用程序中直接使用我的实体 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Calandar.Business.Manager.Data; namespace Calandar.Business.Models.Args { public class SaveExpertArgs { publ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Calandar.Business.Manager.Data;
namespace Calandar.Business.Models.Args
{
public class SaveExpertArgs
{
public ExpertEntity Expert { get; set; }
public SaveExpertArgs(ExpertEntity expert)
{
Expert = expert;
}
}
}
public ExpertEntity SaveExpert(SaveExpertArgs args)
{
string connString = ConfigurationManager.ConnectionStrings["CalendarContainer"].ConnectionString;
using (CalendarContainer dbContext = new CalendarContainer(connString))
{
ExpertEntity expert = (from e in dbContext.ExpertEntities
where e.ExpertIdentifier == args.Expert.ExpertIdentifier
select e).FirstOrDefault();
if (expert == null)
{
args.Expert.ExpertIdentifier = Guid.NewGuid();
dbContext.AddToExpertEntities(args.Expert);
}
else
{
dbContext.ExpertEntities.ApplyCurrentValues(args.Expert);
foreach (TimeSlotEntity t in args.Expert.TimeSlotEntities)
{
dbContext.TimeSlotEntities.ApplyCurrentValues(t);
}
}
dbContext.SaveChanges();
return args.Expert;
}
}
我试图保存我的专家实体,它正在工作,但我不知道如何在我的专家实体中保存我的EntityCollection。有人能帮我吗?试着摆脱其他人:
public ExpertEntity SaveExpert(SaveExpertArgs args)
{
string connString = ConfigurationManager.ConnectionStrings["CalendarContainer"].ConnectionString;
using (CalendarContainer dbContext = new CalendarContainer(connString))
{
ExpertEntity expert = (from e in dbContext.ExpertEntities
where e.ExpertIdentifier == args.Expert.ExpertIdentifier
select e).FirstOrDefault();
if (expert == null)
{
args.Expert.ExpertIdentifier = Guid.NewGuid();
dbContext.AddToExpertEntities(args.Expert);
}
//else
//{
dbContext.ExpertEntities.ApplyCurrentValues(args.Expert);
foreach (TimeSlotEntity t in args.Expert.TimeSlotEntities)
{
dbContext.TimeSlotEntities.ApplyCurrentValues(t);
}
//}
dbContext.SaveChanges();
return args.Expert;
}
}
好的,我找到了更新实体集合的方法 这是我的技巧。我没有找到任何关于这项技术的文档,所以请给我您的反馈
public ExpertEntity SaveExpert(SaveExpertArgs args)
{
string connString = ConfigurationManager.ConnectionStrings["CalendarContainer"].ConnectionString;
using (CalendarContainer dbContext = new CalendarContainer(connString))
{
ExpertEntity expert = (from e in dbContext.ExpertEntities
where e.ExpertIdentifier == args.Expert.ExpertIdentifier
select e).FirstOrDefault();
if (expert == null)
{
args.Expert.ExpertIdentifier = Guid.NewGuid();
dbContext.AddToExpertEntities(args.Expert);
}
else
{
dbContext.ExpertEntities.ApplyCurrentValues(args.Expert);
GenericUpdateEntityCollection(args.Expert.TimeSlotEntities, dbContext);
}
dbContext.SaveChanges();
return args.Expert;
}
}
private void GenericUpdateEntityCollection<T>(EntityCollection<T> collection, ObjectContext dbContext)
where T : EntityObject, new()
{
int count = collection.Count();
int current = 0;
List<T> collectionItemList = collection.ToList();
bool isAdded = false;
while (current < count)
{
Object obj = null;
// Essai de récupéré l'objet dans le context pour le mettre à jour
dbContext.TryGetObjectByKey(collectionItemList[current].EntityKey, out obj);
if (obj == null)
{
// Si l'objet n'existe pas, on en créer un nouveau
obj = new TimeSlotEntity();
// On lui donne l'entity Key du nouvelle objet
((T)obj).EntityKey = collectionItemList[current].EntityKey;
// On l'ajoute au context, dans le timeslot
dbContext.AddObject(((T)obj).EntityKey.EntitySetName, obj);
// On récupère l'objet du context qui à le même entity key que le nouveau recu en pramètre, le but est d'avoir un context d'attacher
dbContext.TryGetObjectByKey(collectionItemList[current].EntityKey, out obj);
// On change l'état de l'objet dans le context pour modifier, car
dbContext.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
// On change l'état de l'objet passé en paramètre pour qu'il soit au même state que celui dans le context
collection.CreateSourceQuery().Context.ObjectStateManager.ChangeObjectState(collectionItemList[current], System.Data.EntityState.Modified);
// On place notre flag pour dire que nous avons ajouter dans le context les donnée
isAdded = true;
}
if (obj != null)
{
// On applique les changements de l'objet passé en parametre à celui dans le context
dbContext.ApplyCurrentValues<T>(((T)obj).EntityKey.EntitySetName,collectionItemList[current]);
// On replace les state des deux objet, celui dans le context et celui passé en parametre à added pour la sauvegarde.
if (isAdded)
{
dbContext.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Added);
collection.CreateSourceQuery().Context.ObjectStateManager.ChangeObjectState(collectionItemList[current], System.Data.EntityState.Added);
}
}
current++;
}
}
public ExpertEntity SaveExpert(SaveExpertArgs args)
{
string connString=ConfigurationManager.ConnectionString[“CalendarContainer”]。ConnectionString;
使用(CalendarContainer dbContext=新的CalendarContainer(connString))
{
expertentityexpert=(来自dbContext.ExpertEntities中的e)
其中e.ExpertIdentifier==args.Expert.ExpertIdentifier
选择e).FirstOrDefault();
if(expert==null)
{
args.Expert.ExpertIdentifier=Guid.NewGuid();
dbContext.AddToExpertEntities(args.Expert);
}
其他的
{
dbContext.expertenties.ApplyCurrentValues(args.Expert);
GenericUpdateEntityCollection(args.Expert.TimeSlotEntities,dbContext);
}
dbContext.SaveChanges();
返回args.Expert;
}
}
私有void GenericUpdateEntityCollection(EntityCollection集合,ObjectContext dbContext)
其中T:EntityObject,new()
{
int count=collection.count();
int电流=0;
List collectionItemList=collection.ToList();
bool isAdded=false;
while(电流<计数)
{
objectobj=null;
//我们的目标是在未来的环境中实现的
dbContext.TryGetObjectByKey(collectionItemList[current].EntityKey,out obj);
if(obj==null)
{
//这是一种新的生活方式
obj=新的TimeSlotEntity();
//关于路易·多恩的《新事物的实体钥匙》
((T)obj).EntityKey=collectionItemList[current].EntityKey;
//在l'ajoute au上下文中,dans le timeslot
dbContext.AddObject(((T)obj.EntityKey.EntitySetName,obj);
//关于背景下的新事物,我认为这是一个新的主题,但这是一个新的主题
dbContext.TryGetObjectByKey(collectionItemList[current].EntityKey,out obj);
//关于更改对象的上下文倒修饰符,car
ObjectStateManager.ChangeObjectState(obj,System.Data.EntityState.Modified);
//关于在环境中改变国家的现状
collection.CreateSourceQuery().Context.ObjectStateManager.ChangeObjectState(collectionItemList[current],System.Data.EntityState.Modified);
//在巴黎圣母院的旗帜上,我们看到了埃文斯·阿尤尔·丹·勒·多内的背景
isAdded=真;
}
如果(obj!=null)
{
//关于环境参数变化的贴花
dbContext.ApplyCurrentValues(((T)obj.EntityKey.EntitySetName,collectionItemList[current]);
//在更换双机状态时,celui dans le context and celui passéen Parameterá增加了sauvegarde。
如果(已添加)
{
ObjectStateManager.ChangeObjectState(obj,System.Data.EntityState.Added);
collection.CreateSourceQuery().Context.ObjectStateManager.ChangeObjectState(collectionItemList[current],System.Data.EntityState.Added);
}
}
电流++;
}
}
不起作用,出现错误:ObjectContext实例已被释放,无法再用于需要连接的操作。您可以尝试在初始加载时使用“Include”命令从专家处获取子对象。我尝试执行exper.TimeSlotEntities.Load();但是,当我使用arg.Expert.TimeSlotEntities时,该对象已断开连接。有没有办法和WrappedRelatedenties合作?