Content

  • Working with List (Controller -> View)
  • Creating New Controller
  • Adding View to Controller Methods
  • Intoduction:Entity Framework Core
  • Core Components of EF Core
  • Entity Framework Core Methods
  • Installing EF Core package

Working of List (Controller -> View)

Example:

Controller File

public IActionResult Index()
{
    List<Students> students = new List<Students>();
    students.Add(new Students { Id=1,Name="Ram",City="Pune"});
    students.Add(new Students { Id=2,Name="Shree",City="Mumbai"});
    students.Add(new Students { Id=2,Name="Subhash",City="Kolkata"});
    students.Add(new Students { Id=3,Name="Narendra",City="Chennai"});
    return View(students);
}

View File

@model List<Students>
@{
    ViewData["Title"] = "Home Page";
}
                    
<div class="text-center">
                    
    <h1 class="display-4">Welcome</h1>
    <h2>Value from Controller</h2>
    @foreach (var student in Model)
    {
        <h2>@student.Name</h2>
        <h3>@student.Id</h3>
        <h3>@student.City</h3>
        <hr />
    }
                       
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

Creating New Controller

Right click on "Contollers" folder -> Add -> Controller -> MVC Contoller - Empty -> Enter Name ->Add

Example:

Controller File

using Microsoft.AspNetCore.Mvc;
namespace Test.Web.Controllers
{
    public class AboutController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult History()
        {
            return View();
        }
    }
}

Adding View to Controller Methods

Right click on Method Name -> Add View -> Razor View

Example : View File (Index.cshtml)

@{
    ViewData["Title"] = "About Company";
}

<div class="container">
    <h1>About Company</h1>
    <div class="row">
        <div class="col-12">
            <p>At [Company Name], we are dedicated to revolutionizing the way [industry/niche] operates. Established in [year], we have been at the forefront of innovation, consistently delivering cutting-edge solutions to meet the evolving needs of our clients.</p>
            <p>Our mission is simple: to empower businesses with the tools and technologies they need to thrive in today's competitive landscape. Whether you're a small startup or a multinational corporation, we tailor our services to suit your unique requirements, ensuring maximum efficiency and profitability.</p>
            <a asp-action="History" class="btn btn-primary">Company History</a>
        </div>
    </div>
</div>

Example : View File (History.cshtml)

@{
    ViewData["Title"] = "Company History";
}

<h1>Company History</h1>
<div class="container">
    <div class="row">
        <div class="col-12">
            <p>
                Since our inception in [year], [Company Name] has been synonymous with innovation and excellence in the [industry/niche] sector. What began as a humble startup with a vision to disrupt the status quo has evolved into a leading force in the industry, shaping the way businesses operate and thrive in the modern world.

            </p>
            <p>In our early days, [Founder's Name], our visionary founder, recognized a gap in the market and seized the opportunity to fill it. Armed with determination and a relentless pursuit of excellence, [Founder's Name] laid the foundation for what would become [Company Name]'s legacy of innovation.</p>
            <a asp-action="Index" class="btn btn-primary">Back</a>
        </div>
    </div>
</div>

Intoduction:Entity Framework Core

Entity Framework Core (EF Core) is a powerful and versatile object-relational mapping (ORM) framework developed by Microsoft. It is designed to simplify the process of working with relational databases in .NET applications by providing a high-level abstraction over the underlying database schema.

  • Object-Relational Mapping (ORM): EF Core enables developers to work with database entities as .NET objects, abstracting away the complexities of database interactions. This allows developers to focus on business logic rather than database management tasks.
  • Cross-Platform and Lightweight: EF Core is a cross-platform framework that runs on .NET Core, .NET Framework, and Xamarin. It is also lightweight compared to its predecessor, Entity Framework 6, making it suitable for use in a wide range of applications, including web, desktop, and mobile.
  • Code-First Approach: With EF Core, developers can define their database schema using C# or VB.NET code, known as the "code-first" approach. This allows for greater flexibility and control over the database model, making it easier to work with databases that evolve over time.
  • Support for Multiple Database Providers: EF Core supports a variety of database providers, including SQL Server, PostgreSQL, MySQL, SQLite, and more. This flexibility allows developers to choose the database that best suits their application requirements without being tied to a specific platform.
  • LINQ Integration: EF Core seamlessly integrates with Language Integrated Query (LINQ), allowing developers to write expressive and type-safe queries directly against their entity classes. This simplifies the process of querying and manipulating data, leading to more maintainable and readable code.
  • Migration Support: EF Core includes built-in support for database migrations, making it easy to evolve the database schema alongside the application code. Developers can use migrations to automatically apply schema changes, ensuring database consistency across different environments.
  • Performance Optimization: EF Core offers various features and optimizations to improve performance, such as query caching, lazy loading, and compiled queries. These optimizations help minimize database round-trips and improve overall application performance.

Core Components of EF Core

  • DbContext: It is the primary class that is responsible for interacting with the database. It represents a session with the database and can be used to query and save instances of your entities.
  • DbSet<TEntity>: It is a property on the DbContext that allows querying and saving of entities. Each DbSet corresponds to a table in the database.
  • Entity: A class that represents a table in the database.
  • Migration: A way to manage and apply schema changes to the database, like creating tables or adding new columns.

Entity Framework Core Methods

Method Description
Add Adds a new entity to the context.
Remove Removes an entity from the context.
Update Updates the properties of an existing entity in the context.
SaveChanges Saves all changes made in the context to the underlying database.
Find Finds an entity with the given primary key values.
FirstOrDefault/SingleOrDefault/First/Single Executes a query and returns the first matching entity or null if no matching entity is found.
ToList/ToListAsync/ToArray Executes a query and returns the result as a list or array.
Include Specifies related entities to be included in the query results.
OrderBy/ThenBy Specifies ordering criteria for query results.
FromSqlRaw Executes a SQL query directly on the database and returns the results as entities.

Installing EF Core package

Right click on Project Name -> Manage NuGet Packages -> Search - Microsoft.EntityFrameworkCore.SqlServer -> Select Version - 7.0.11 -> Install