SQL Server

SQL Server

SQL Server Provider

Microsoft SQL Server is a relational database management system (RDBMS) that supports a wide variety of transaction processing, business intelligence, and analytic applications in corporate IT environments.
  • It is the default database provider which is available when you install Entity Framework Extensions​
  • It allows Entity Framework Core to be used with Microsoft SQL Server (including SQL Azure).

Install Entity Framework Core

Let's create a new application using the Console App (.NET Core) template, and install Microsoft.EntityFrameworkCore. 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!
You can also install it by right-clicking on your project in Solution Explorer and select Manage Nuget Packages...
Search for Microsoft.EntityFrameworkCore and install the latest version by pressing the install button. It doesn't have additional logic that won't apply to all scenarios.
For example, EF Core will need to know what database or datastore you plan on working with and who those providers are in individual packages.

Register EF Core Provider

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!
Now, you are ready to start your application.

Create Data Model

Model is a collection of classes to interact with the database.
  • A model stores data that is retrieved according to the Controller's commands and displayed in the View.
  • It can also be used to manipulate the data to implement the business logic.
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!
There's a one-to-many relationship between Author and Book entities. In other words, an author can write any number of books, and a book can be written by only one author.

Create Database Context

The database context class provides the main functionality to coordinate Entity Framework with a given data model.
  • You create this class by deriving from the System.Data.Entity.DbContext class.
  • In your code, you specify which entities are included in the data model.
  • You can also customize certain Entity Framework behaviors.
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!
In EF Core, the DbContext has a virtual method called OnConfiguring which will get called internally by EF Core.
  • It will pass in an optionsBuilder instance which can be used to configure options for the DbContext.
  • The optionsBuilder has UseSqlServer method which expects a connection string as a parameter.
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.
1
using (var context = new BookStore())
2
{
3
context.Database.EnsureCreated();
4
​
5
var authors = new List<Author>
6
{
7
new Author
8
{
9
FirstName ="Carson",
10
LastName ="Alexander",
11
BirthDate = DateTime.Parse("1985-09-01"),
12
Books = new List<Book>()
13
{
14
new Book { Title = "Introduction to Machine Learning"},
15
new Book { Title = "Advanced Topics on Machine Learning"},
16
new Book { Title = "Introduction to Computing"}
17
}
18
},
19
new Author
20
{
21
FirstName ="Meredith",
22
LastName ="Alonso",
23
BirthDate = DateTime.Parse("1970-09-01"),
24
Books = new List<Book>()
25
{
26
new Book { Title = "Introduction to Microeconomics"}
27
}
28
},
29
new Author
30
{
31
FirstName ="Arturo",
32
LastName ="Anand",
33
BirthDate = DateTime.Parse("1963-09-01"),
34
Books = new List<Book>()
35
{
36
new Book { Title = "Calculus I"},
37
new Book { Title = "Calculus II"}
38
}
39
}
40
};
41
​
42
context.Authors.AddRange(authors);
43
context.SaveChanges();
44
}
45
​
46
using (var context = new BookStore())
47
{
48
var list = context.Authors
49
.Include(a => a.Books)
50
.ToList();
51
​
52
foreach (var author in list)
53
{
54
Console.WriteLine(author.FirstName + " " + author.LastName);
55
​
56
foreach (var book in author.Books)
57
{
58
Console.WriteLine("\t" + book.Title);
59
}
60
}
61
}
Copied!
If you run the application, you will see that authors and books are successfully inserted into the database.
Last modified 4mo ago