Thursday, February 6, 2020

Web API Core Startup.cs page

This is one of an important class that introduces core changes to the web API terminology in .net core.

A few highlights are as follows.




How to create a Web API Core and Entity Framework Core Project in Visual Studio Code

Install .NET Core
Use one of the methods below to create a web api project

Run the following command to create a web api project from a template from the command prompt

  • Go to the project location using cd <path>
    • CD C:\Users\david\Documents\\Projects\mot_service
  • Then run following command
    • dotnet new webapi -o theprojectname -f netcoreapp3.1
    • cd theprojectname
  • Then run following command that will open the project in Visual Studio Code
    • code -r ../theprojectname


In visual studio code, Go to Terminal




  • dotnet new webapi -o theprojectname -f netcoreapp3.1


  • Open the project from the editor or run in the terminal :
    • code -r ../theprojectname
Install the following Extensions


Add following packages using the commands in the terminal or command prompt, command palet
 dotnet add package Microsoft.EntityFrameworkCore.Sqlite



  • Command Pallet:


Create your DbContext class and Model classes

Use the following commands to create the database.

  • To add migrations - dotnet ef migrations add InitialCreate
  • To create database - dotnet ef database update
To see the sqllite file use SQL Lite Studio or sqlitebrowser

Wednesday, February 5, 2020

Entity Framework Core Visual Studio Code : Error when adding migrations



Error
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET Core program, but dotnet-ef does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

Solution:
Make sure .NET Core is installed and added to environment variable path.
https://docs.microsoft.com/en-us/dotnet/core/tools/troubleshoot-usage-issues


Friday, June 24, 2016

Identity (===) operator vs Equality (==) operator

Identity (===) operator
  • Do not do type conversion 
  • If a different type it just returns false. Types must be same to validate.

Equality (==) operator
  • Do type conversion 

**Other than that behaves identically.


Read C# dictionary JSON from JavaScript



C#
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var dic = new Dictionary<string, string>();
        dic.Add("Id", "0001");
        dic.Add("Name", "Harry");
        dic.Add("Age", "30");
        dic.Add("Address", "Los angellies");
        var jss = new JsonSerializer();
        var jstring = JsonConvert.SerializeObject(dic);
        Console.WriteLine(jstring);
    }
}

JavaScript with Testable HTML

<!DOCTYPE html>
<html>
<body>
<h2>Create Object from JSON String</h2>
<p id="demo"></p>

<script>
function validate(nativeJsonObj) {

inputObject = JSON.parse(nativeJsonObj);
var emptyFields = new Array();

var filedsToValidate = ["Name", "Age", "Address"];
var i = 0;
   for (var input in inputObject)
   {
       if (inputObject.hasOwnProperty(input))
       {
          //alert(filedsToValidate[i]);
          if(!(!filedsToValidate[i]))
          {
             if(filedsToValidate[i] == input)
             {
                if(checkValue(inputObject[input])==false)
                {  
                    emptyFields.push(input);             

                }
             } 

         i++;
          }
          else
           alert(filedsToValidate[i]);
       }
   }
            document.getElementById("demo").innerHTML += emptyFields + "   " +
            emptyFields.length + "</br>"
}

function checkValue(valueToCheck)
{
    var validated = true;
    if(!valueToCheck)
        validated = false;
   
    return validated;
}


var input = '{"Id":"0001","Name":"","Age":"a","Address":"s"}';

validate(input);
</script>

</body>
</html>