public void Seed() { centerRep = new BaseRepository <Center>(_dbContext); centerRep.AddEntity(new Center() { Name = "Hyderabad", CenterType = _Context.CenterTypes.FirstOrDefault(ct => ct.ShortName == "ST"), CenterRefId = _Context.Centers.FirstOrDefault(c => c.Name == "India").Id, }); }
/// <summary> /// 发布博客 /// Author Anton /// DateTime 2014年11月10日 20:30:30 /// </summary> /// <param name="post"></param> /// <returns></returns> public int PublishPost(BlogPost post) { if (post == null) { throw new ArgumentNullException("post"); } var postRepository = new BaseRepository <BlogPost>(); var postSubjectRepository = new BaseRepository <BlogPostSubject>(); var postTagRepository = new BaseRepository <BlogPostTag>(); int postId; try { postId = postRepository.AddEntity(post, true).Id; } catch (Exception ex) { throw new Exception("发布博客失败:" + ex.Message); } //添加多个标签 foreach (int tagId in post.BlogTagIds) { postTagRepository.AddEntity( new BlogPostTag { PostId = postId, BlogTagId = tagId, CreateTime = DateTime.Now, Deleted = false }); } //postSubjectRepository.AddEntity( // new BlogPostSubject // { // PostId = postId, // SubjectId = 1, // CreateTime = DateTime.Now, // Deleted = false // }); try { return(DbCommit.Commit()); } catch (Exception ex) { throw new Exception("添加博客标签失败:" + ex.Message); } }
/// <summary> /// 发布博客 /// Author Anton /// DateTime 2014年11月10日 20:30:30 /// </summary> /// <param name="post"></param> /// <returns></returns> public int PublishPost(BlogPost post) { if (post == null) throw new ArgumentNullException("post"); var postRepository = new BaseRepository<BlogPost>(); var postSubjectRepository = new BaseRepository<BlogPostSubject>(); var postTagRepository = new BaseRepository<BlogPostTag>(); int postId; try { postId = postRepository.AddEntity(post, true).Id; } catch (Exception ex) { throw new Exception("发布博客失败:"+ex.Message); } //添加多个标签 foreach (int tagId in post.BlogTagIds) { postTagRepository.AddEntity( new BlogPostTag { PostId = postId, BlogTagId = tagId, CreateTime = DateTime.Now, Deleted = false }); } //postSubjectRepository.AddEntity( // new BlogPostSubject // { // PostId = postId, // SubjectId = 1, // CreateTime = DateTime.Now, // Deleted = false // }); try { return DbCommit.Commit(); } catch (Exception ex) { throw new Exception("添加博客标签失败:"+ex.Message); } }
public void AddRecipe(RecipeInfoViewModel viewModel) { // Add recipe entity var model = new Recipe { Name = viewModel.Name, Description = viewModel.Description, Directions = viewModel.Directions, CreatedDate = DateTime.Now }; BaseRepository.AddOrUpdateEntity(model); viewModel.RecipeId = model.RecipeId; // Add ingredient mappings foreach (var ingredient in viewModel.Ingredients) { if (ingredient.IngredientId == 0) { var ing = BaseRepository.GetEntityList <Ingredient>(new List <ICriterion> { Restrictions.Eq(Ingredient.COL_NAME, ingredient.Name) }).FirstOrDefault(); if (ing == null) { ing = new Ingredient { Name = ingredient.Name }; BaseRepository.AddOrUpdateEntity(ing); } BaseRepository.AddEntity(new RecipeIngredientMapping { IngredientId = ing.IngredientId.Value, RecipeId = viewModel.RecipeId.Value, Unit = ingredient.Unit, Quantity = ingredient.Quantity }); } else { BaseRepository.AddEntity(new RecipeIngredientMapping { IngredientId = ingredient.IngredientId, RecipeId = viewModel.RecipeId.Value, Unit = ingredient.Unit, Quantity = ingredient.Quantity }); } } // Add tag mappings if (viewModel.TagIds != null) { foreach (var id in viewModel.TagIds) { var mapping = new RecipeTagMapping { TagId = id, RecipeId = viewModel.RecipeId.Value }; BaseRepository.AddEntity(mapping); viewModel.Tags.Add(new TagInfo { TagId = id, Name = mapping.Tag.Name, Description = mapping.Tag.Name }); } } // Add tag mappings foreach (var tag in viewModel.Tags) { if (tag.TagId == 0) { var t = BaseRepository.GetEntityList <Tag>(new List <ICriterion> { Restrictions.Eq(Tag.COL_NAME, tag.Name) }).FirstOrDefault(); if (t == null) { t = new Tag { Name = tag.Name }; BaseRepository.AddOrUpdateEntity(t); } BaseRepository.AddEntity(new RecipeTagMapping { TagId = t.TagId.Value, RecipeId = viewModel.RecipeId.Value }); } else { BaseRepository.AddEntity(new RecipeTagMapping { TagId = tag.TagId, RecipeId = viewModel.RecipeId.Value }); } } // Add images foreach (var image in viewModel.Images) { BaseRepository.AddEntity(new Image { Url = image.Url, RecipeId = viewModel.RecipeId, Description = image.Description, CreatedDate = DateTime.Now }); } }
public T AddEntity(T entity) { return(_baseRepository.AddEntity(entity)); }