Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using EF_tutorial;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.ComponentModel.DataAnnotations;
- using System.Data.Entity;
- using Newtonsoft.Json;
- using System.IO;
- namespace EF_tutorial
- {
- class Program
- {
- static void Main(string[] args)
- {
- USER sampleUser1 = new USER
- {
- Username = "Jakinator",
- Password = "IllBeBack"
- };
- USER sampleUser2 = new USER
- {
- Username = "ILoveKittens",
- Password = "meow"
- };
- USER sampleUser3 = new USER
- {
- Username = "SoccerMom",
- Password = "MyKidsAnHonorStudent"
- };
- BLOG_POSTS samplePost = new BLOG_POSTS
- {
- DatePosted = DateTime.Now,
- Title = "Why Terminator is the best movie ever!",
- Body = "Terminator is the best movie ever because it's the only one I've ever seen. It was so awesome that when I finished watching it I vowed to never watch another movie again."
- };
- using (var blogo = new MyEntities())
- {
- blogo.BLOG_POSTS.Add(samplePost);
- }
- POST_COMMENTS sampleComment1 = new POST_COMMENTS
- {
- UserId = sampleUser1.Id,
- DatePosted = DateTime.Now,
- Body = "First! I'm always the first to comment on my own blog posts. I'm just awesome like that."
- };
- POST_COMMENTS sampleComment2 = new POST_COMMENTS
- {
- UserId = sampleUser2.Id,
- DatePosted = DateTime.Now,
- Body = "Jake I completely disagree that Terminator is the best movie ever for one reason: There were no kittens in Terminator. If there were kittens, then maybe..."
- };
- POST_COMMENTS sampleComment3 = new POST_COMMENTS
- {
- UserId = sampleUser3.Id,
- DatePosted = DateTime.Now,
- Body = "I would never let my kids watch horrible movies like this. They should be at soccer practice or doing homework. That's why my kids have all straight A's and are all honor students."
- };
- using (var com = new MyEntities())
- {
- com.USERS.Add(sampleUser1);
- //com.USERS.Add(sampleUser2);
- //com.USERS.Add(sampleUser3);
- com.POST_COMMENTS.Add(sampleComment1);/*
- com.POST_COMMENTS.Add(sampleComment2);
- com.POST_COMMENTS.Add(sampleComment3);*/
- com.BLOG_POSTS.Add(samplePost);
- com.SaveChanges();
- }
- using (var dej = new MyEntities())
- {
- var blogs = (from b in dej.USERS
- select b).ToList();
- Console.WriteLine("ALL POSTS:");
- foreach (var blog in blogs)
- Console.WriteLine(" " + blog.Username);
- }
- using (var dej = new MyEntities())
- {
- var bos = (from s in dej.POST_COMMENTS
- select s).ToList();
- foreach (var sd in bos)
- Console.WriteLine("" + sd.Body);
- }
- Console.WriteLine("asdasdasd");
- dbToJson1();
- Console.ReadLine();
- }
- public class MYUSERS
- {
- public string user { get; set; }
- public string pass { get; set; }
- }
- public class MYPOSTS
- {
- //public int id { get; set; }
- public int uid { get; set; }
- public System.DateTime date { get; set; }
- public string ptitle { get; set; }
- public string pbody { get; set; }
- }
- public class MYCOMMENTS
- {
- //public int Id { get; set; }
- public int BlogPostId { get; set; }
- public int UserId { get; set; }
- public System.DateTime DatePosted { get; set; }
- public string Body { get; set; }
- }
- /*
- public static void dbToJson()
- {
- var dej = new MyEntities();
- IQueryable<MySystem> sysList = from s in dej.USERS
- select new MySystem()
- {
- user = s.Username,
- pass = s.Password
- };
- MySystem sys = sysList.First();
- string joson = JsonConvert.SerializeObject(sys);
- File.WriteAllText(@"json.json", joson.ToString());
- }*/
- public static void dbToJson1()
- {
- var dej = new MyEntities();
- IQueryable<MYCOMMENTS> sysList2 = from s in dej.POST_COMMENTS
- select new MYCOMMENTS()
- {
- //Id = s.Id,
- BlogPostId = s.BlogPostId,
- UserId = s.UserId,
- DatePosted = s.DatePosted,
- Body = s.Body
- };
- IQueryable<MYPOSTS> sysList1 = from s in dej.BLOG_POSTS
- select new MYPOSTS()
- {
- //id = s.Id,
- uid = s.UserID,
- date = s.DatePosted,
- ptitle = s.Title,
- pbody = s.Body
- };
- IQueryable<MYUSERS> sysList = from s in dej.USERS
- select new MYUSERS()
- {
- user = s.Username,
- pass = s.Password
- };
- List<MYCOMMENTS> com = new List<MYCOMMENTS>(sysList2);
- List<MYPOSTS> pos = new List<MYPOSTS>(sysList1);
- List<MYUSERS> use = new List<MYUSERS>(sysList);
- string juse = JsonConvert.SerializeObject(use);
- string jpos = JsonConvert.SerializeObject(pos);
- string jcom = JsonConvert.SerializeObject(com);
- File.WriteAllText(@"json.json", juse.ToString());
- File.AppendAllText(@"json.json",jpos.ToString() + Environment.NewLine);
- File.AppendAllText(@"json.json", jcom.ToString() + Environment.NewLine);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement