boeschenstein / angular9-dotnetcore-ef-sql

EF Core + SQL Server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EF Core + SQL Server

Goal

Add .NET EF Core to .NET Core WebApi project

Content

Prerequisites

  • SQL Server
  • Recommended: SQL Server Management Studio (SSMS)
  • Your user must be able to login to the sql server and create databases and tables
  • Alternatively you can change the connectionstring from "Trusted_Connection=True" to user/password (DBContext)

WebApi

Create a .NET Core WebApi project. Or use this: https://github.com/boeschenstein/angular9-dotnetcore3

Install EF Core

open cmd in the folder of the csproj file and enter:

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

DBContext

Create a Folder EF and add 3 classes:

namespace MyBackend.EF
{
    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        // replace BloggingEFTest, if you want a different name for your database
        protected override void OnConfiguring(DbContextOptionsBuilder options)
            => options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=BloggingEFTest;Trusted_Connection=True;MultipleActiveResultSets=true;");
    }
}
namespace MyBackend.EF
{
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        // Referential Integrity: with this line, EF will generate a foreign key
        public List<Post> Posts { get; } = new List<Post>();
    }
}
namespace MyBackend.EF
{
    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
}

Create Database from Context

dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet ef migrations add InitialCreate
dotnet ef database update

Start SQL Server Management Studio (SSMS), login to your database server and check the new database.

Database Access (CRUD)

To check th implementation, run the following Code.

For simplicity reasons, I added the following code to my Get() in WeatherForecastController.

using MyBackend.EF;
using System.Linq;

...

using (var db = new BloggingContext())
{
    // Create
    Console.WriteLine("Inserting a new blog");
    db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
    db.SaveChanges();

    // Read
    Console.WriteLine("Querying for a blog");
    var blog = db.Blogs
        .OrderBy(b => b.BlogId)
        .First();

    // Update
    Console.WriteLine("Updating the blog and adding a post");
    blog.Url = "https://devblogs.microsoft.com/dotnet";
    blog.Posts.Add(
        new Post
        {
            Title = "Hello World",
            Content = "I wrote an app using EF Core!"
        });
    db.SaveChanges();

    // Delete
    Console.WriteLine("Delete the blog");
    db.Remove(blog);
    db.SaveChanges();
}

Please remove this code after your test: It is not recommended to access database directly in controller!

Congratulations, you just added EF Core to your ASP.NET WebAPI project!

Tips and Tricks

EF Command line Tools + Migrations

dotnet tool install --global dotnet-ef

Update tooling

dotnet tool update --global dotnet-ef

To add a new migration

dotnet ef migrations add {name}

rem Add project, if solution contains many
dotnet ef migrations add {name} --context MyDbContext --startup-project <MyProjectName>

To remove last migration file from project

dotnet ef migrations remove

rem Add project, if solution contains many
dotnet ef migrations remove --context MyDbContext --startup-project <MyProjectName>

Revert to old migration

dotnet ef database update {old_migration}

rem Add project, if solution contains many
dotnet ef database update {old_migration} --context MyDbContext --startup-project <MyProjectName>

To update DB to the latest migration

dotnet ef database update

rem Add project, if solution contains many
dotnet ef database update  --context MyDbContext --startup-project <MyProjectName>

What's next

Additional Information

Links

Current Versions

  • Visual Studio 2019 16.5.3
  • .NET core 3.1
  • npm 6.14.4
  • node 12.16.1
  • Angular CLI 9.1

About

EF Core + SQL Server


Languages

Language:HTML 56.0%Language:C# 23.0%Language:TypeScript 16.9%Language:JavaScript 3.9%Language:CSS 0.2%