Tuesday, November 28, 2023

MVC - C# Reference

? A question mark (?) is used to denote a nullable type. A nullable type can represent all the values of its underlying non-nullable value type plus an additional null value. This is particularly useful when dealing with value types, which cannot normally be assigned a null value. 

int? nullableInt = 42;  // Valid assignment

nullableInt = null;    // Valid assignment

DateTime? nullableDateTime = DateTime.Now;  // Valid assignment

nullableDateTime = null;                    // Valid assignment

?? The ?? operator, also known as the null-coalescing operator, is used for handling null values in a concise way. It provides a way to return a default value when the left-hand operand is null. 

string proName = pro?[0]?.Name ?? "Default Value";

The #pragma warning disable directive is used in C# to disable specific compiler warnings: #pragma warning disable CS8602

Target-typed new expressions are a feature introduced in C# 9.0. They allow you to omit the type in the new expression when the type can be inferred from the surrounding context. This helps reduce redundancy in your code and makes it more concise.        

// Without target-typed new expression

List<string> names = new List<string>();

// With target-typed new expression (C# 9.0 and later)

List<string> names = new(); 

The update-database command is part of the Entity Framework Migrations workflow and is crucial for keeping the database schema in sync with your code changes. It's a powerful tool that automates the process of evolving the database as your application evolves, making it easier to manage changes to your data model over time.

        Applying Database Migrations/Creating the Initial Database/Applying Subsequent     Migrations/Rolling Back Migrations

In Entity Framework, the DbContext class provides a set of methods to interact with the underlying database using LINQ queries. Here are some common methods associated with DbSet that allow you to perform various operations:

  1. Query Operations:

    • FirstOrDefault, SingleOrDefault: Retrieve the first or a single entity that satisfies a condition.
    • Where: Filter entities based on a condition.
    • OrderBy, OrderByDescending, ThenBy, ThenByDescending: Order entities based on one or more properties.
  2. Insert/Update Operations:

    • Add, AddRange: Add a new entity or a collection of entities to the context.
    • Attach: Attaches an entity or a disconnected graph of entities to the context.
    • Update: Marks an entity or entities as modified.
    • Remove, RemoveRange: Remove an entity or a collection of entities from the context.
  3. Save Changes:

    • SaveChanges, SaveChangesAsync: Persist changes made in the context to the underlying database.
  4. Bulk Operations:

    • Entity Framework Core (EF Core) has introduced extensions like BulkInsert, BulkUpdate, BulkDelete for handling bulk operations efficiently.
  5. Raw SQL Queries:

    • FromSqlRaw, FromSqlInterpolated: Execute raw SQL queries.