public override void UpdatePost(Post post) { var context = new BlogsEntities(); var obj = context.tblBlogs_Posts.Include("Categories").Where(item => item.Id == post.Id).FirstOrDefault(); post.CopyToObject(obj); obj.Categories.Clear(); if (post.Categories.Count > 0) { var categories = context.tblBlogs_Categories.Where( Utils.BuildContainsExpression<tblBlogs_Categories, int>(item => item.Id, post.Categories.Select(item => item.Id))); foreach (var category in categories) { obj.Categories.Add(category); } } context.SaveChanges(); }
public override int InsertPost(Post post) { var context = new BlogsEntities(); var obj = new tblBlogs_Posts(); post.CopyToObject(obj); if (post.Categories != null) { foreach (var category in post.Categories) { var item = new tblBlogs_Categories {Id = category.Id}; context.AttachTo("tblBlogs_Categories", item); obj.Categories.Add(item); } } context.AddTotblBlogs_Posts(obj); context.SaveChanges(); return obj.Id; }