Entity Framework Core Archives - Learn Smart Coding https://blogs.learnsmartcoding.com/tag/entity-framework-core/ Everyone can code! Wed, 22 Dec 2021 16:08:04 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.2 209870635 Entity Framework Core commands explained with examples https://blogs.learnsmartcoding.com/2021/12/22/entity-framework-core-commands-explained-with-examples/ https://blogs.learnsmartcoding.com/2021/12/22/entity-framework-core-commands-explained-with-examples/#respond Wed, 22 Dec 2021 16:08:04 +0000 https://karthiktechblog.com/?p=999 Introduction Entity Framework Core commands are useful and make our life a bit easy to execute a few tasks. EF Core is popular Entity Framework data access technology. Entity Framework (EF) Core is a lightweight, extensible and open source. EF Core can serve as an object-relational mapper (O/RM) and it supports many database engines. I […]

The post Entity Framework Core commands explained with examples appeared first on Learn Smart Coding.

]]>
Introduction

Entity Framework Core commands are useful and make our life a bit easy to execute a few tasks. EF Core is popular Entity Framework data access technology. Entity Framework (EF) Core is a lightweight, extensible and open source. EF Core can serve as an object-relational mapper (O/RM) and it supports many database engines.

Entity Framework Core commands explained with examples

I will cover all the options from the three commands.

Commands:

  • database Commands to manage the database.
  • dbcontext Commands to manage DbContext types.
  • migrations Commands to manage migrations.

Short video tutorial

Installation

.NET Core CLI

Use the following .NET Core CLI command from the operating system’s command line to install or update the EF Core SQL Server provider

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Visual Studio NuGet Package Manager Console

Install-Package Microsoft.EntityFrameworkCore.SqlServer


Get the .NET Core CLI tools

dotnet ef must be installed as a global or local tool.

Also, Install the latest Microsoft.EntityFrameworkCore.Design package

dotnet tool install --global dotnet-ef

dotnet add package Microsoft.EntityFrameworkCore.Design

Refer to how to install Entity Framework Core for more information.

Demo App Explained

For the purpose of the demo, I have a sample restaurant app created using DOTNET CORE. Using the app, we will learn how to use all the dotnet ef commands.

Project structure:

The sample app project has five projects in it. Each serves its purpose and it satisfies the separation of concerns. In the real world, the project has more than one layer hence I choose to demo in a similar structure.

Entity Framework Core commands project structure

Entity Classes

 FoodMenus { get; set; }
    }

    public class FoodImages
    {
        public int Id { get; set; }
        public byte[] Image { get; set; }
        public string Mime { get; set; }
        public string ImageName { get; set; }
        public bool IsActive { get; set; }
        public int? FoodMenusId { get; set; }
        public virtual FoodMenus FoodMenus { get; set; }
    }

    public class FoodMenus
    {
        public int Id { get; set; }
        [Required]
        [MinLength(5), MaxLength(100)]
        public string Name { get; set; }
        [Required]
        [MinLength(100), MaxLength(5000)]
        public string Description { get; set; }
        public decimal Price { get; set; }        
        public bool IsActive { get; set; }
        public short? CategoryId { get; set; }
        public virtual Category Category { get; set; }
        public int? CuisineId { get; set; }
        public virtual Cuisine Cuisine { get; set; }
        public virtual List FoodImages { get; set; }
    }

Now, Let’s get started in exploring each command from dotnet ef command.

Using .NET CORE CLI : Command Prompt

  1. I have opened the database layer project’s location in the command prompt. E.g. “KarthikTechBlog.Restaurant.Data”.

2. To check entity framework core is installed and ready to use, type “dotnet ef” in the command prompt and you will see similar or same details as shown in the image.

dotnet entity framework core

EF Core Commands in Action

migrations : add

dotnet ef migrations add InitialCreate -s ../KarthikTechBlog.Restaurant.API/KarthikTechBlog.Restaurant.API.csproj

To create migrations from the application, we use the command migration add followed by name of the migration. -s “location of the startup project” is to specify the startup project. This is required for the project to build and generate the migration.

dotnet ef migrations add

To generate SQL script for the Entity models, use the below command. Remember the script can be generated only when migrations are created.

migrations : script

dotnet ef migrations script  -s ../KarthikTechBlog.Restaurant.API/KarthikTechBlog.Restaurant.API.csproj
dotnet ef migrations script

migrations: list

This lists all the migrations present in the project.

dotnet ef migrations list  -s ../KarthikTechBlog.Restaurant.API/KarthikTechBlog.Restaurant.API.csproj

migrations: remove

Rollbacks the latest migration.

dotnet ef migrations remove-s ../KarthikTechBlog.Restaurant.API/KarthikTechBlog.Restaurant.API.csproj

database: bundle

Creates an executable to update the database.

dotnet ef migrations bundle -s ../KarthikTechBlog.Restaurant.API/KarthikTechBlog.Restaurant.API.csproj

database : update

This command updates the database with the details present in the migration. If the database is not present, it will create one for us.

dotnet ef database update -s ../KarthikTechBlog.Restaurant.API/KarthikTechBlog.Restaurant.API.csproj

database: drop

Deletes the database. some of the options are

  1. --force or -f which is used to just delete without confirmation.
  2. --dry-run option show which database would be dropped, but don’t drop it.
dotnet ef database drop

dbcontext: scaffold

Generates code for a DbContext and entity types for a database. In order for this command to generate an entity type, the database table must have a primary key.

To scaffold all schemas and tables and puts the new files in the Models folder.

dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Shopping;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models


For more options, visit MSDN

dbcontext: optimize

Generates a compiled version of the model used by the DbContext. Added in EF Core 6.

dotnet ef dbcontext optimize

dbcontext: script

Generates a SQL script from the DbContext. Bypasses any migrations.

dotnet ef dbcontext script

Summary

This post covers Entity Framework Core commands and their usage with examples. I hope you enjoyed and learned something new today.

The post Entity Framework Core commands explained with examples appeared first on Learn Smart Coding.

]]>
https://blogs.learnsmartcoding.com/2021/12/22/entity-framework-core-commands-explained-with-examples/feed/ 0 999
Entity Framework Core dbcontextoptionsbuilder does not contain a definition for usesqlserver – Solved https://blogs.learnsmartcoding.com/2020/03/04/entity-framework-core-dbcontextoptionsbuilder-does-not-contain-a-definition-for-usesqlserver-solved/ https://blogs.learnsmartcoding.com/2020/03/04/entity-framework-core-dbcontextoptionsbuilder-does-not-contain-a-definition-for-usesqlserver-solved/#respond Wed, 04 Mar 2020 16:19:04 +0000 https://karthiktechblog.com/?p=406 Introduction In this post, I will show how to solve the error that shows “Entity Framework Core dbcontextoptionsbuilder does not contain a definition for UseSqlServer” with ASP.Net Core 3 and 3.1 framework. Overview We wanted to connect our ASP.NET Core application with MS SQL. To do this, we configure a connection in the startup class. […]

The post Entity Framework Core dbcontextoptionsbuilder does not contain a definition for usesqlserver – Solved appeared first on Learn Smart Coding.

]]>
Introduction

In this post, I will show how to solve the error that shows “Entity Framework Core dbcontextoptionsbuilder does not contain a definition for UseSqlServer” with ASP.Net Core 3 and 3.1 framework.

Overview

We wanted to connect our ASP.NET Core application with MS SQL. To do this, we configure a connection in the startup class.

 // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContextPool(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DbContext")));

            services.AddControllers();
        }

However, when I did the same setup with ASP.NET Core 3.1, I ran into issue. Compiler complained that “Entity Framework Core dbcontextoptionsbuilder does not contain a definition for UseSqlServer”.

Entity Framework Core dbcontextoptionsbuilder does not contain a definition for usesqlserver

Solution

After digging into the issue, I figured out there was a small change done from Microsoft and to make this work you need to just add a package called “Microsoft.EntityFrameworkCore.SqlServer“.

Install NuGet Package Microsoft.EntityFrameworkCore.SqlServer

    
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" />

Or

PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer

Just remove or comment the lines that are showing as error and install the package. That’s it!

dbcontextoptionsbuilder does not contain a definition for usesqlserver solved

Also see how to Implement create, read, update, and delete functionalities using Entity Framework Core

Conclusion

Thanks for reading this piece. As you can see, the method is not moved to a different namespace from ASP.NET Core 3.0 version and installing this extra package will solve the problem. Let me know in the comments if you have questions.

The post Entity Framework Core dbcontextoptionsbuilder does not contain a definition for usesqlserver – Solved appeared first on Learn Smart Coding.

]]>
https://blogs.learnsmartcoding.com/2020/03/04/entity-framework-core-dbcontextoptionsbuilder-does-not-contain-a-definition-for-usesqlserver-solved/feed/ 0 406