-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Program.cs
117 lines (97 loc) · 4.11 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Examples.Models;
using MongoDB.Entities;
using System;
using System.Diagnostics;
using System.Linq;
namespace Examples
{
public static class Program
{
private static void Main(string[] args)
{
//BASIC INITIALIZATION
//
////.Net Core
new DB("bookshop", "localhost", 27017);
////Asp.Net Core
//services.AddMongoDBEntities("DatabaseName", "HostAddress", PortNumber);
//ADVANCED INITIALIZATION
//
////.Net Core
//new DB(new MongoClientSettings()
//{
// Server = new MongoServerAddress("localhost", 27017),
// Credential = MongoCredential.CreateCredential("Demo", "username", "password")
//}, "Demo");
////Asp.Net Core
//services.AddMongoDBEntities(
// new MongoClientSettings()
// {
// Server = new MongoServerAddress("HostAddress", PortNumber),
// Credential = MongoCredential.CreateCredential("DatabaseName", "UserName", "Password")
// },
// "DatabaseName");
var stopWatch = new Stopwatch(); stopWatch.Start();
//SAVING
var book1 = new Book { Title = "The Power Of Now" }; book1.Save();
var book2 = new Book { Title = "I Am That I Am" }; book2.Save();
var author1 = new Author { Name = "Eckhart Tolle" }; author1.Save();
var author2 = new Author { Name = "Nisargadatta Maharaj" }; author2.Save();
var genre1 = new Genre { Name = "Self Help" }; genre1.Save();
//EMBEDDING DOCUMENTS
book1.Review = new Review { Stars = 5, Reviewer = "New York Times" }; //Review does not inherit from Entity.
book1.RelatedAuthor = author2.ToDocument();
book1.OtherAuthors = (new Author[] { author1, author2 }).ToDocuments();
book1.Save();
//RELATIONSHIPS
//
/////One-To-One (Embedded)
book1.RelatedAuthor = author2;
////One-To-One (Referenced)
book1.MainAuthor = author1.ToReference();
book1.Save();
////One-To-Many (Embedded)
book2.OtherAuthors = new Author[] { author1, author2 };
book2.Save();
////One-To-Many (Referenced)
book2.Authors.Add(new[] { author1, author2 }); //References are automatically saved. No need to save the entity.
////Many-To-Many (Referenced)
genre1.AllBooks.Add(book1);
genre1.AllBooks.Add(book2);
//QUERIES
//
////Main collections
var author = (from a in DB.Queryable<Author>()
where a.Name.Contains("Eckhart")
select a).FirstOrDefault();
////Reference collections
var authors = (from a in book2.Authors.ChildrenQueryable()
select a).ToArray();
////Get entity of referenced relationship
var mainAuthor = (from b in DB.Queryable<Book>()
where b.Title == book1.Title
select b.MainAuthor)
.SingleOrDefault()
.ToEntity();
////Collection shortcut
var result = from a in author.Queryable()
select a;
//DELETE
//
////Delete single entity
book1.RelatedAuthor.Delete();
book1.RelatedAuthor = null;
book1.Save();
book1.Delete(); //References pointing to this entity are also deleted
////Delete multiple entities
book2.OtherAuthors.DeleteAll();
book2.OtherAuthors = null;
book2.Save();
////Delete by lambda expression
DB.Delete<Book>(b => b.ID == book2.ID);
//THE END
Console.WriteLine($"All operations completed in {stopWatch.Elapsed.TotalSeconds:0.00} seconds.");
Console.ReadLine();
}
}
}