更新Entity Framework中的现有EntityCollection [英] Update existing EntityCollection in Entity Framework
本文介绍了更新Entity Framework中的现有EntityCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用我的实体系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用Calandar.Business.Manager.Data;
命名空间Calandar.Business.Models.Args
{
public class SaveExpertArgs
{
public ExpertEntity Expert {get;组;
public SaveExpertArgs(ExpertEntity expert)
{
Expert = expert;
}
}
}
public ExpertEntity SaveExpert(SaveExpertArgs args)
{
string connString = ConfigurationManager.ConnectionStrings [CalendarContainer] .ConnectionString;
使用(CalendarContainer dbContext = new CalendarContainer(connString))
{
ExpertEntity expert =(从e在dbContext.ExpertEntities
中,e.ExpertIdentifier == args。 Expert.ExpertIdentifier
选择e).FirstOrDefault();
if(expert == null)
{
args.Expert.ExpertIdentifier = Guid.NewGuid();
dbContext.AddToExpertEntities(args.Expert);
}
else
{
dbContext.ExpertEntities.ApplyCurrentValues(args.Expert);
foreach(argsExpert.TimeSlotEntities中的TimeSlotEntity t)
{
dbContext.TimeSlotEntities.ApplyCurrentValues(t);
}
}
dbContext.SaveChanges();
return args.Expert;
}
}
我尝试保存我的专家实体,但我不知道如何在我的专家实体中保存我的EntityCollection。有些身体可以帮助我吗?
解决方案
确定我找到了我可以更新我的实体集合。
有我的技术。我没有找到有关该技术的任何文档,所以给我你的反馈
public ExpertEntity SaveExpert(SaveExpertArgs args)
{
string connString = ConfigurationManager.ConnectionStrings [CalendarContainer]。ConnectionString;
使用(CalendarContainer dbContext = new CalendarContainer(connString))
{
ExpertEntity expert =(从e在dbContext.ExpertEntities
中,其中e.ExpertIdentifier == args.Expert.ExpertIdentifier
选择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>集合,ObjectContext dbContext)
其中T:EntityObject,new()
{
int count = collection.Count();
int current = 0;
列表< T> collectionItemList = collection.ToList();
bool isAdded = false;
while(current< count)
{
Object obj = null;
// Essai deré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 encréerun 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);
// onrécupèrel'objet du context quiàlemêmeentity key que le nouveau recu enpramètre,le but est d'avoir un context d'attacher
dbContext.TryGetObjectByKey(collectionItemList [current]。 EntityKey,out obj);
// on change l'étatde l'objet dans le context pour modifier,car
dbContext.ObjectStateManager.ChangeObjectState(obj,System.Data.EntityState.Modified);
//更改l'étatde l'objetpasséenparamètrepour qu'il soit aumêmestate 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 lesdonnée
isAdded = true;
}
if(obj!= null)
{
// on applique les changements de l'objetpasséen parametreàcelui dans le context
dbContext.ApplyCurrentValues< T(((T)obj).EntityKey.EntitySetName,collectionItemList [current]);
//在替换les state des deux objet,celui dans le context et celuipassé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 ++;
}
}
I try to work with link to entity, and i want to work directly with my entity in my application.
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;
}
}
I try to save my expert entity and it's working, but i dont know how to save my EntityCollection in my expert Entity. some body can help me ?
解决方案
Ok i found how i could update my entity collection.
There is my technique. I did not find any documentation on the technique, so give me your feed back
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++;
}
}
这篇关于更新Entity Framework中的现有EntityCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文