更新实体框架中现有的 EntityCollection
我尝试使用实体链接,并且我想直接在应用程序中使用我的实体。
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 保存在我的专家实体中。有人可以帮助我吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试摆脱其他:
Try get rid of the else:
好吧,我找到了如何更新我的实体集合。
这里面有我的技术。我没有找到任何有关该技术的文档,所以请给我您的反馈
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