Quantcast
Channel: Uncategorized – Amit Patriwala
Viewing all articles
Browse latest Browse all 9

.Net Core Entity Framework Code First Approach Using Class Library Project

$
0
0

With reference to Getting Started with EF Core, I am using the MSSQL server.

Steps

  1. Create a new project
  2. Create Class Library
  3. Install Entity Framework
  4. Create Model
  5. Register context with dependency injection
  6. Create database

Step 1: Create a New Project – Choose ASP.NET Core Web Application (.NET Core)

You can follow the above Microsoft Document to create project using either CLI or Visual Studio.

I have given project name “WebApplication1”

Step 2: Add a New Project into Solution – Class Library (.NET Core)

I have given project name “ClassLibrary1”

Step 3: Install Entity Framework

I have install three package using NuGet

  1. Microsoft.EntityFrameworkCore
  2. Microsoft.EntityFrameworkCore.SqlServer – For SQL SERVER
  3. Microsoft.EntityFrameworkCore.Tools – It is command line tools for EF Core that include commands for Package Manager console

Step 4: Create Model

As per the above reference document, I have created the Model

using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace ClassLibrary1
{
    public class BloggingContext : DbContext

    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        public BloggingContext(DbContextOptions<BloggingContext> options) : base(options) { }

    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; } = new List<Post>();
    }

    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; }
    }
}

Note that added the BloggingContext and inherited the DbContext in it. We have added the Microsoft.EntityFrameworkCore namespace.

Also, we have added the constructor

public BloggingContext(DbContextOptions<BloggingContext> options) : base(options) { }

Step 5: Register context with dependency injection

Before registering the context, let’s reference the library to our Web application project.

Go to WebApplication1 then right click, Add -> Project Reference and add ClassLibrary1

Now go to -> Startup.cs file

You can see the, ConfigureServices method, we need to add AddDbContext method which uses Dependency Injection (DI) to register BloggingContext as a service.

 // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen();

            //SQL Connection
            var connection = @"Server=SQLServer;Database=EFCoreStudentDb;Trusted_Connection=True;";
            services.AddDbContext<BloggingContext>(options => options.UseSqlServer(connection));
        }

You can see that I have use the Swagger so injected it’s dependency here, you can remove it.

Step 6: Create database

Now, open the Package Manager Console in Visual Studio, go to Tools NuGet Package Manager > Package Manager Console

Now, let’s create a database by using migrations.

Command: Add-Migration Initial

After successfully implement the command, you can check class library project, You can see the Migrations folder with the classes.

Now, we need to apply new migration to the database.

Command: Update-Database

After successfully implement the command, you can check the database, it created the new database and table in it.

Now, you can create the REST API.

Happy Programming!!


Viewing all articles
Browse latest Browse all 9

Latest Images

Trending Articles





Latest Images