public async Task SaveCommentsPolicy(CommentsPolicy policy) { using (var transaction = db.Database.BeginTransaction()) { var query = db.CommentsPolicies.Where(x => x.CourseId == policy.CourseId); if (query.Any()) { db.CommentsPolicies.Remove(query.First()); await db.SaveChangesAsync(); } db.CommentsPolicies.Add(policy); await db.SaveChangesAsync(); transaction.Commit(); } }
public CommentsPolicy GetCommentsPolicy(string courseId) { var policy = db.CommentsPolicies.FirstOrDefault(x => x.CourseId == courseId); if (policy == null) policy = new CommentsPolicy { CourseId = courseId, }; return policy; }