Today I’ll show you how to create a small console application with a Step by step: .NET Core and Entity Framework Core example.
First be aware of the following prerequisites:
Now let’s start:
Create a folder for your new project
Open a command promt an run
1mkdir efcore
Create the project
1cd efcore
2dotnet new
Create a settings file
Create an appsettings.json file to hold your connection string information. We’ll be using **SQLite **for this example, so add these lines:
1{
2 "ConnectionStrings": {
3 "Sample": "Data Source=sample.db"
4 }
5}
Modify the project file
Modify the project.json to add the EntityFrameworkCore dependencies and also specify that the appsettings.json file must be copied to the output (**buildOptions **section) so it becomes available to the application once you build it.
1{
2 "version": "1.0.0-*",
3 "buildOptions": {
4 "debugType": "portable",
5 "emitEntryPoint": true,
6 "copyToOutput": {
7 "include": "appsettings.json"
8 }
9 },
10 "dependencies": {
11 "Microsoft.Extensions.Configuration": "1.0.0",
12 "Microsoft.Extensions.Configuration.Json": "1.0.0",
13 "Microsoft.EntityFrameworkCore": "1.0.0",
14 "Microsoft.EntityFrameworkCore.Sqlite": "1.0.0"
15 },
16 "frameworks": {
17 "netcoreapp1.0": {
18 "dependencies": {
19 "Microsoft.NETCore.App": {
20 "type": "platform",
21 "version": "1.0.0"
22 }
23 },
24 "imports": "dnxcore50"
25 }
26 }
27}
Restore packages
You just modified the project.json file with new dependencies so please restore the packages with the following command:
1dotnet restore
Create the Entity Framework context
Create a SampleContext.cs file and copy the following code
1namespace ConsoleApplication
2{
3 using Microsoft.EntityFrameworkCore;
4
5 /// <summary>
6 /// The entity framework context with a Students DbSet
7 /// </summary>
8 public class StudentsContext : DbContext
9 {
10 public StudentsContext(DbContextOptions<StudentsContext> options)
11 : base(options)
12 { }
13
14 public DbSet<Student> Students { get; set; }
15 }
16
17 /// <summary>
18 /// A factory to create an instance of the StudentsContext
19 /// </summary>
20 public static class StudentsContextFactory
21 {
22 public static StudentsContext Create(string connectionString)
23 {
24 var optionsBuilder = new DbContextOptionsBuilder<StudentsContext>();
25 optionsBuilder.UseSqlite(connectionString);
26
27 // Ensure that the SQLite database and sechema is created!
28 var context = new StudentsContext(optionsBuilder.Options);
29 context.Database.EnsureCreated();
30
31 return context;
32 }
33 }
34
35 /// <summary>
36 /// A simple class representing a Student
37 /// </summary>
38 public class Student
39 {
40 public Student()
41 {
42 }
43
44 public int Id { get; set; }
45
46 public string Name { get; set; }
47
48 public string LastName { get; set; }
49 }
50}
Modify Program.cs
Replace the contents of the Program.cs file with the following code
1namespace ConsoleApplication
2{
3 using System;
4 using Microsoft.Extensions.Configuration;
5
6 public class Program
7 {
8 public static void Main(string[] args)
9 {
10 // Enable to app to read json setting files
11 var builder = new ConfigurationBuilder()
12 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
13
14 var configuration = builder.Build();
15
16 // Get the connection string
17 string connectionString = configuration.GetConnectionString("Sample");
18
19 // Create a Student instance
20 var user = new Student() { Name = "Carlos", LastName = "Mendible" };
21
22 // Add and Save the student in the database
23 using (var context = StudentsContextFactory.Create(connectionString))
24 {
25 context.Add(user);
26 context.SaveChanges();
27 }
28
29 Console.WriteLine($"Student was saved in the database with id: {user.Id}");
30 }
31 }
32}
Build
Build the application with the following command
1dotnet build
Run
You are good to go so run the application
1dotnet run
You can get the code here: https://github.com/cmendible/dotnetcore.samples/tree/main/efcore.sqlite.console
Hope it helps!
Comments