This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.EntityFrameworkCore; | |
namespace TestCosmos | |
{ | |
// Console App (.NET Core) | |
//<ItemGroup> | |
//<PackageReference Include = "Microsoft.EntityFrameworkCore.Cosmos" Version="2.2.0-preview3-35497" /> | |
//<PackageReference Include = "Microsoft.EntityFrameworkCore.Tools" Version="2.2.4"/> | |
//</ItemGroup> | |
public class Book | |
{ | |
public string ISBN { get; set; } | |
public string Title { get; set; } | |
public string Author { get; set; } | |
public string Language { get; set; } | |
public int Pages { get; set; } | |
public virtual Publisher Publisher { get; set; } | |
} | |
public class Publisher | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public virtual ICollection<Book> Books { get; set; } | |
} | |
public class LibraryContext : DbContext | |
{ | |
public DbSet<Book> Book { get; set; } | |
public DbSet<Publisher> Publisher { get; set; } | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
string endPoint = "https://localhost:8081"; | |
string authKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; | |
string dbName = "LibraryDb"; | |
optionsBuilder.UseCosmos(endPoint, authKey, dbName); | |
} | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
base.OnModelCreating(modelBuilder); | |
modelBuilder.Entity<Publisher>(entity => | |
{ | |
entity.HasKey(e => e.Id); | |
entity.Property(e => e.Name).IsRequired(); | |
}); | |
modelBuilder.Entity<Book>(entity => | |
{ | |
entity.HasKey(e => e.ISBN); | |
entity.Property(e => e.Title).IsRequired(); | |
entity.HasOne(d => d.Publisher) | |
.WithMany(p => p.Books); | |
}); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
InsertData(); | |
PrintData(); | |
} | |
private static void InsertData() | |
{ | |
using (var context = new LibraryContext()) | |
{ | |
// Creates the database if not exists | |
context.Database.EnsureCreated(); | |
// Adds a publisher | |
var publisher = new Publisher | |
{ | |
Id = Guid.NewGuid().ToString(), | |
Name = "Mariner Books" | |
}; | |
context.Publisher.Add(publisher); | |
// Adds some books | |
context.Book.Add(new Book | |
{ | |
ISBN = "978-0544003415", | |
Title = "The Lord of the Rings", | |
Author = "J.R.R. Tolkien", | |
Language = "English", | |
Pages = 1216, | |
Publisher = publisher | |
}); | |
context.Book.Add(new Book | |
{ | |
ISBN = "978-0547247762", | |
Title = "The Sealed Letter", | |
Author = "Emma Donoghue", | |
Language = "English", | |
Pages = 416, | |
Publisher = publisher | |
}); | |
// Saves changes | |
context.SaveChanges(); | |
} | |
} | |
private static void PrintData() | |
{ | |
// Gets and prints all books in database | |
using (var context = new LibraryContext()) | |
{ | |
var books = context.Book | |
.Include(p => p.Publisher); | |
foreach (var book in books) | |
{ | |
var data = new StringBuilder(); | |
data.AppendLine($"ISBN: {book.ISBN}"); | |
data.AppendLine($"Title: {book.Title}"); | |
data.AppendLine($"Publisher: {book.Publisher.Name}"); | |
Console.WriteLine(data.ToString()); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment