-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
59 lines (47 loc) · 2.37 KB
/
Program.cs
File metadata and controls
59 lines (47 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Linq;
namespace EFGetStarted
{
class Program
{
static void Main(string[] args)
{
using(var db = new BloggingContext())
{
// remove everything to make program runs idempotent
foreach(var b in db.Blogs)
{
db.Remove(b);
}
db.SaveChanges();
Console.WriteLine("Let us add some posts");
var b1 = new BloggingContext.Blog { Url = "http://myspace/article1.html" };
b1.Posts.Add(new BloggingContext.Post { Title = "title1", Content = "Laoreet urna semper ut morbi enim ad" });
b1.Posts.Add(new BloggingContext.Post { Title = "title2", Content = "Lacus luctus ultrices faucibus facilisis est non donec" });
db.Add(b1);
var b2 = new BloggingContext.Blog { Url = "http://myspace/article1.html" };
b2.Posts.Add(new BloggingContext.Post { Title = "title1", Content = "Tempus suspendisse mattis elit varius" });
b2.Posts.Add(new BloggingContext.Post { Title = "title2", Content = "Rutrum venenatis dictum arcu aenean" });
b2.Posts.Add(new BloggingContext.Post { Title = "title3", Content = "Platea donec aenean non venenatis aenean arcu hendrerit aenean etiam" });
db.Add(b2);
// write to db
db.SaveChanges();
Console.WriteLine("Let's query a post containing word 'mattis'");
var q = from b in db.Blogs
from p in b.Posts
where p.Content.Contains("mattis") select p;
Console.WriteLine("Post with title: '" + q.First().Title + "' contains the word 'mattis'");
// let us now modify that post
q.First().Title = "modified title";
db.SaveChanges();
Console.WriteLine("Let's now print all posts");
var all_posts = from b in db.Blogs
from p in b.Posts
select p;
foreach (var post in all_posts) {
Console.WriteLine("post title: '" + post.Title + "', content: " + post.Content);
}
}
}
}
}