本文整理汇总了C#中Db.Entry方法的典型用法代码示例。如果您正苦于以下问题:C# Db.Entry方法的具体用法?C# Db.Entry怎么用?C# Db.Entry使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Db
的用法示例。
在下文中一共展示了Db.Entry方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveOrUpdate
public SaveResult<User> SaveOrUpdate(User u)
{
using (var db = new Db())
{
if (u.Id != 0)
{
db.Entry(u).State = EntityState.Modified;
}
else
{
db.Users.Add(u);
}
db.SaveChanges();
return new SaveResult<User>(true, u);
}
}
示例2: SaveOrUpdate
public SaveResult<Event> SaveOrUpdate(Event e)
{
using (var db = new Db())
{
if (e.Id != 0)
{
var ev = db.Events.FirstOrDefault(x => x.Id == e.Id);
ev.Title = e.Title;
ev.StartTime = e.StartTime;
ev.EndTime = e.EndTime;
ev.IsPrivate = e.IsPrivate;
ev.Description = e.Description;
ev.Location = e.Location;
ev.FinishedGettingPosts = e.FinishedGettingPosts;
ev.IsActive = e.IsActive;
ev.PollIsActive = e.PollIsActive;
if (e.Guests != null)
{
foreach (var i in e.Guests)
{
i.Guest = db.Users.FirstOrDefault(x => x.Id == i.GuestId);
if (i.Id != 0)
{
db.Entry<Invitation>(i).State = EntityState.Modified;
}
else
{
ev.Guests.Add(i);
db.Entry<Invitation>(i).State = EntityState.Added;
}
}
}
if (e.Posts != null)
{
foreach (var p in e.Posts)
{
if (p.Id == 0)
{
p.From = db.Users.FirstOrDefault(x => x.Id == p.From.Id);
ev.Posts.Add(p);
db.Entry<Post>(p).State = EntityState.Added;
db.Entry<User>(p.From).State = EntityState.Modified;
}
if (p.Comments != null)
{
foreach (var c in p.Comments)
{
if (c.Id == 0)
{
c.From = db.Users.FirstOrDefault(x => x.Id == c.From.Id);
ev.Posts.FirstOrDefault(x => x.Id == p.Id).Comments.Add(c);
db.Entry<Post>(c).State = EntityState.Added;
db.Entry<User>(c.From).State = EntityState.Modified;
}
}
}
}
}
if (e.Votes != null)
{
foreach (var v in e.Votes)
{
if (v.Id != 0)
{
db.Entry<Vote>(v).State = EntityState.Modified;
}
else
{
ev.Votes.Add(v);
db.Entry<Vote>(v).State = EntityState.Added;
}
}
}
db.Entry<Event>(ev).State = EntityState.Modified;
db.Entry<User>(ev.Owner).State = EntityState.Unchanged;
}
else
{
e.Owner = db.Users.FirstOrDefault(x => x.Id == e.Owner.Id);
db.Events.Add(e);
}
db.SaveChanges();
}
return new SaveResult<Event>(true, e);
}