-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
40 lines (32 loc) · 1.36 KB
/
Program.cs
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
using EFCoreValueObjectPredicateExample;
using var db = new BloggingContext();
// Note: This sample requires the database to be created before running.
Console.WriteLine($"Database path: {db.DbPath}.");
// Create
Console.WriteLine("Inserting a new blog");
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
db.SaveChanges();
// Read
Console.WriteLine("Querying for a blog");
var blog = db.Blogs
.OrderBy(b => b.BlogId)
.First();
// Update
Console.WriteLine("Updating the blog and adding a post");
blog.Url = "https://devblogs.microsoft.com/dotnet";
blog.Posts.Add(
new Post { Title = "Hello World", Content = "I wrote an app using EF Core!", Status = Status.Active });
blog.Posts.Add(
new Post { Title = "2nd post", Content = "Lets see if this works!", Status = Status.Active });//THIS BREAKS! with the following exception: SQLite Error 19: 'NOT NULL constraint failed: Posts.Status'.
db.SaveChanges();
// Query using primitive type
Console.WriteLine("This works: Querying for a post (Using primitive type)");
var posts1 = db.Posts
.Where(p => p.Status.Name.Equals(Status.Active.Name)).ToList();
// Query using Value Object
Console.WriteLine("THIS BREAKS! Querying for a post (Using Value Object)");
var posts2 = db.Posts.Where(p => p.Status.Equals(Status.Active)).ToList();
// Delete
Console.WriteLine("Delete the blog");
db.Remove(blog);
db.SaveChanges();