static void Main(string[] args) { using (var db = new ForumContext()) { var user = new User() { login = "******", password = "******", role = Role.ADMIN }; var topic = new Topic() { title = "Java", createdAt = DateTime.Now }; var post = new Post() { content = "Java 8 jest fajna!", createdAt = DateTime.Now, }; post.topic = topic; post.user = user; topic.posts.Add(post); user.topics.Add(topic); user.posts.Add(post); db.users.Add(user); db.topics.Add(topic); db.posts.Add(post); db.SaveChanges(); } }
public List<Post> GetPosts(Topic topic) { List<Post> results = null; using (var db = new ForumContext()) { var query = from p in db.posts where p.topID == topic.topID select p; results = query.ToList<Post>(); foreach (Post post in results) { var user = post.user; string login = null; if (user != null) login = user.login; } } return results; }
public void AddPost(String content, Topic topic, User user) { using (var db = new ForumContext()) { var post = new Post(); post.content = content; post.createdAt = DateTime.Now; var findTopic = db.topics.Find(topic.topID); if (findTopic != null) { findTopic.posts.Add(post); post.topic = findTopic; } var findUser = db.users.Find(user.usrID); if (findUser != null) { findUser.posts.Add(post); post.user = findUser; } db.SaveChanges(); } }
public void AddTopic(String title, User user) { using (var db = new ForumContext()) { var topic = new Topic(); topic.title = title; topic.createdAt = DateTime.Now; var founded = db.users.Find(user.usrID); if (founded != null) founded.topics.Add(topic); topic.author = founded; db.topics.Add(topic); db.SaveChanges(); } }