Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
namespace TestMySql
{
// Console App (.NET Core)
//<ItemGroup>
//<PackageReference Include = "Microsoft.EntityFrameworkCore.Tools" Version="2.2.4"/>
//<PackageReference Include = "MySql.Data.EntityFrameworkCore" Version="8.0.16" />
//</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 dataSource = "localhost";
string catalog = "LibraryDb";
string connectionString =
$"server = {dataSource}; port = 3306; database = {catalog}; uid = root; password = hilarisbest1!";
optionsBuilder.UseMySQL(connectionString);
}
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
You can’t perform that action at this time.