Monday, 2 September 2024

Migration comments in .net 8 or .net core

1. Install latest Ef core, Design and related packages

2. Update db connection config in Web/Api/Azure funtion APP and set as startup project.

3. Once Code first Entities Created in library project then Open package manger console => Select corresponding library project.

4. Add Migration

PM> Add-Migration migration_name_description

Build started...

Build succeeded.

5. To undo this action, use Remove Migration

PM> Remove-Migration migration_name_description

6. Update Database

PM> update-database –verbose

Build started...

Build succeeded.

It will generate DML & DDL scripts. 

Friday, 16 August 2024

.net Package upgrade with bindings

 The solution to this is actually quite simple and elegant.

  1. Remove all your binding redirects in Web.config / app.config;
  2. Go to Package Manager Console;
  3. Enter the command Add-BindingRedirect (you can also specify a target project using -ProjectName "SpecificProject");
  4. All necessary binding redirects are generated;
  5. Run your application and see if it works properly. If not, add any missing binding redirects that the command missed.

 

From < https://stackoverflow.com/questions/50036791/find-unused-unnecessary-assemblybinding-redirects>

Thursday, 2 May 2024

Difference between IEnumerable,IQueryable VS List

 First the difference between List<T>, IEnumerable<T> and IQueryable<T>.

 The content of List<T> will always be loaded into memory, because it uses an array in the background.


The content of IEnumerable<T> will only load the requested item into the memory. Compiler sets up a state machine during compiling a method with IEnumerable<T> that has a yield return T. So only one item at a time will be loaded into memory in this case.


IQueryable<T> is "special" version of IEnumerable<T>. Specifically designed to query data sources, and implemented by data providers. Like EF, NHibernate. So when you tell EF to fetch the data with .ToListAsync<T>() EF will only then go the database and execute the generated query. See the docs for more info about IQueryable<T>.

Wednesday, 27 December 2023

C# Enum Value and Display name Extension

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Web;

namespace Helper.Extensions
{
    public static class EnumExtensions
    {
        public static string GetEnumValue(this Enum enumValue)
        {
            var type = enumValue.GetType();
            var info = type.GetField(enumValue.ToString());
            if (info == null) return string.Empty;

            var da = (EnumMemberAttribute[])
                    (info.GetCustomAttributes(typeof(EnumMemberAttribute), false));

            if (da.Length > 0)
                return da[0].Value;
            else
                return string.Empty;
        }

        public static string GetDisplayName(this Enum enumValue)
        {
            return enumValue.GetType()
                            .GetMember(enumValue.ToString())
                            .First()
                            .GetCustomAttribute<DisplayAttribute>()
                            .GetName();
        }

    }
}

Friday, 4 August 2023

C# simple example for recursive call

using System;
class HelloWorld {
  static void Main() {
      var result=print(0);
    Console.WriteLine(result);
  }
  public static string print(int count)
  {
      return print2(count);
  }
  public static string print2(int count)
  {
      if(count <= 2)
      {
          Console.WriteLine(count);
          count++;
          //recursive call upto 3count
          return print(count);
      }
     
      return "Recursive final return";
  }
}

//output like below
0
1
2
Recursive final return