Ejemplo n.º 1
0
        public Post InsertPost(int blog_id, string title, string text, string[] tags)
        {
            try
            {
                post = new Post {
                    blog_id = blog_id, title = title, text = text
                };
                dbcontext.Add(post); // track posts
                dbcontext.SaveChanges();

                foreach (var item in tags)
                {
                    tag = new Tag {
                        name = item
                    };
                    dbcontext.Entry(tag).State = dbcontext.Tags.Any(t => t.name == item)
                        ? EntityState.Unchanged
                        : EntityState.Added;

                    dbcontext.SaveChanges();

                    var ptJoin = new PostTag {
                        post_id = post.id, tag_id = tag.id
                    };
                    dbcontext.Add(ptJoin);
                    dbcontext.SaveChanges();
                }
                return(post);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        public async Task <bool> InsertUser(string name)
        {
            user = new User {
                name = name
            };
            dbcontext.Add(user);                              // tracking user object

            var isSaved = await dbcontext.SaveChangesAsync(); // insert into DB

            //dbcontext.Entry(user).State = EntityState.Detached; // remove user entity from tracking/memory

            return(isSaved >= 1 ? true : false);
        }