Console

Console

A Console application is a program designed to be used via a text-only computer interface, such as a text terminal, the command line interface, etc.
  • A user typically interacts with a console application using only a keyboard and display screen, as opposed to GUI applications, which normally require the use of a mouse or other pointing device.
  • Many console applications such as command-line interpreters are command-line tools, but numerous text-based user interface (TUI) programs also exist.

Create a Console App

To start, we will create a Console application project. The project type comes with all the template files you will need before adding anything. Let's open Visual Studio 2019. If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.
On the start window, choose Create a new project.
On the Create a new project window, enter or type console in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list. Select the Console App (.NET Core) template, and then choose Next.
In the Configure your new project window, type or enter EFCore5InConsoleApp in the Project name box and click on the Create button.
Visual Studio opens your new project and includes the default "Hello World" code in your project.

Install Entity Framework Core

To use Entity Framework Core we need to install Microsoft.EntityFrameworkCore library. It is available as a nuget package and you can install it using Nuget Package Manager.
In the Package Manager Console window, enter the following command.
1
PM> Install-Package Microsoft.EntityFrameworkCore
Copied!
For SQL Server LocalDB, which is installed with Visual Studio, we need to install Microsoft.EntityFrameworkCore.SqlServer and will get all the packages required for EF Core.
1
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
Copied!

Create a Data Model and Database Context

To create a data model for our application, we will start with the following two entities.
1
public class Author
2
{
3
public int AuthorId { get; set; }
4
public string FirstName { get; set; }
5
public string LastName { get; set; }
6
public DateTime BirthDate { get; set; }
7
public List<Book> Books { get; set; }
8
}
9
​
10
public class Book
11
{
12
public int BookId { get; set; }
13
public string Title { get; set; }
14
public Author Author { get; set; }
15
}
Copied!
The database context class provides the main functionality to coordinate Entity Framework with a given data model. So, let's add a new BookStore class which will inherit the DbContext class.
1
public class BookStore : DbContext
2
{
3
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
4
{
5
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=BookStoreDb;");
6
}
7
​
8
public DbSet<Author> Authors { get; set; }
9
public DbSet<Book> Books { get; set; }
10
}
Copied!
Now, we are done with the required classes and database creation, let's add some authors and book records to the database and then retrieve them as shown below.
1
static void Main(string[] args)
2
{
3
using (var context = new BookStore())
4
{
5
context.Database.EnsureCreated();
6
​
7
var authors = new List<Author>
8
{
9
new Author
10
{
11
FirstName ="Carson",
12
LastName ="Alexander",
13
BirthDate = DateTime.Parse("1985-09-01"),
14
Books = new List<Book>()
15
{
16
new Book { Title = "Introduction to Machine Learning"},
17
new Book { Title = "Advanced Topics on Machine Learning"},
18
new Book { Title = "Introduction to Computing"}
19
}
20
},
21
new Author
22
{
23
FirstName ="Meredith",
24
LastName ="Alonso",
25
BirthDate = DateTime.Parse("1970-09-01"),
26
Books = new List<Book>()
27
{
28
new Book { Title = "Introduction to Microeconomics"}
29
}
30
},
31
new Author
32
{
33
FirstName ="Arturo",
34
LastName ="Anand",
35
BirthDate = DateTime.Parse("1963-09-01"),
36
Books = new List<Book>()
37
{
38
new Book { Title = "Calculus I"},
39
new Book { Title = "Calculus II"}
40
}
41
}
42
};
43
​
44
context.Authors.AddRange(authors);
45
context.SaveChanges();
46
}
47
​
48
using (var context = new BookStore())
49
{
50
var list = context.Authors
51
.Include(a => a.Books)
52
.ToList();
53
​
54
foreach (var author in list)
55
{
56
Console.WriteLine(author.FirstName + " " + author.LastName);
57
​
58
foreach (var book in author.Books)
59
{
60
Console.WriteLine("\t" + book.Title);
61
}
62
}
63
}
64
}
Copied!
If you run the application, you will see that authors and books are successfully inserted into the database and also print on the console window.
Last modified 4mo ago