Friday, April 28, 2023

Sql Constraints? #sql

 1. DEFAULT Constraint: Provides a default value for a column when none is specified.

2. UNIQUE Constraint: Ensures that all the values in a column are different.

3. PRIMARY Key: Uniquely identifies each row/record in a database table.

4. FOREIGN Key: Uniquely identifies a row/record in any another database table.

5. CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy

certain conditions.

6. INDEX: Used to create and retrieve data from the database very quickly.

7. NOT NULL Constraint: Ensures that a column cannot have a NULL value.



CREATE TABLE EMPLOYEES

(

   [Name] VARCHAR(50) PRIMARY KEY,

   ID INT NOT NULL,

   IsActive BIT DEFAULT(1),

   AGE INT UNIQUE

)


SELECT ID,IsActive,AGE,[Name] FROM EMPLOYEES


INSERT INTO EMPLOYEES(ID,[Name],AGE) VALUES(1,'Mohit17',40)


INSERT INTO EMPLOYEESALARY(SALARY,NAME_PR) VALUES(100000,'Mohit1722')


Select *from EMPLOYEESALARY


--CREATE TABLE EMPLOYEESALARY

--(

--    SALARY INT NOT NULL,

-- NAME_PR VARCHAR(50) FOREIGN KEY REFERENCES EMPLOYEES([Name])

--)


CREATE TABLE EmployeeAddress

(

    AGE INT NOT NULL CHECK(AGE >=18)

)


SELECT *from EmployeeAddress


INSERT INTO EmployeeAddress(AGE) VALUES(18)



CREATE INDEX INDEX_AGE

ON EmployeeAddress(AGE)

What is An Assembly ? #assembly #csharp

 An Assembly is a basic building block of .Net Framework applications. It is basically a compiled code that can be executed by the CLR. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. An Assembly can be a DLL or exe depending upon the project that we choose.

 
Assemblies are basically the following two types:
  1. Private Assembly
  2. Shared Assembly
1. Private Assembly
 
It is an assembly that is being used by a single application only. Suppose we have a project in which we refer to a DLL so when we build that project that DLL will be copied to the bin folder of our project. That DLL becomes a private assembly within our project. Generally, the DLLs that are meant for a specific project are private assemblies.
 
2. Shared Assembly
 
Assemblies that can be used in more than one project are known to be a shared assembly. Shared assemblies are generally installed in the GAC. Assemblies that are installed in the GAC are made available to all the .Net applications on that machine.
 
However, there are two more types of assemblies in .Net, Satellite Assembly, and Shared Assembly.
 
GAC
 
GAC stands for Global Assembly Cache. It is a memory that is used to store the assemblies that are meant to be used by various applications.
 
Every computer that has CLR installed must have a GAC. GAC is a location that can be seen at “C:\Windows\assembly” for .Net applications with frameworks up to 3.5. For higher frameworks like 4 and 4.5 the GAC can be seen at:
 
“C:\Windows\Microsoft.NET\assembly\GAC_MSIL”.
 
Installing an assembly into the GAC
 
For installing an assembly into the GAC we first need to generate strong names for the assembly because only a strong name assembly is installed in the GAC. All other assemblies are known as weak named assemblies and they cannot be stored in the GAC.
 
For generating a strong name assembly we first create a console application type project. In that solution, we will add one more project (Class Library).
 
Class Library
 
In the Class Library project, I created a class Student that contains a method that returns a string. Now I will add a reference of the Class Library to my Console application project and will use this method.
 
If we see the bin folder of our Console Application project then we will see that the ClassLibrary DLL is present there (It will come when we build our project). This DLL is a Private Assembly now.
 
Console application project
 
Generating Strong Name
 
To make our assembly strongly named we need to generate a unique key-value pair, for that we need to open the Visual Studio command prompt. We can open that from the Start button by going to "All programs" -> "Visual Studio 2012" (or any version that you have)  -> "Visual Studio Tools" -> "Developer Command Prompt for Visual Studio" and run that as an administrator.
 
Command Prompt
 
Then type the command sn –k “Path where you want that key to be generated”
 
command
 
This will generate the unique key pair that we will use in our application.
 
Now move to the Visual Studio Class Library Project and open the AssemblyInfo.cs file that is residing in the Properties folder of your project. In the bottom of that file add a new attribute that refers to the key pair file that we have just created.
 
[assembly: AssemblyKeyFile("D:\\strongname.snk")] 
 
attribute
 
Now build your Class Library project and ensure it builds successfully. If it builds then it means that your ClassLibrary DLL has become strongly typed. If you want to check that then open the Visual Studio command prompt, copy the path of the "bin.Debug" folder of your ClassLibrary project. Now paste the path into the command prompt with the cd command (refer to the following screenshot). Now type the command sn –T “ClassLibrary.dll”. After typing this command you will get a public key token that indicates that your assembly is Strongly Named and it is ready to install in the GAC.
 
install in GAC
 
Now to install the assembly in the cache type the following command in the command prompt:
 
gacutil –i "Name of the Class Library"
 
install assembly in cache
 
Now the Assembly is successfully installed on the system. If you want to see that you can see that in the location “C:\Windows\Microsoft.NET\assembly\GAC_MSIL”.
 
Note: If you are using the framework greater than 4.0 then you can search the preceding given path but if you are using a framework lesser than 4.0 then your assembly will be installed at “C:\Windows\assembly”.
 
installed
 
 

Thursday, April 27, 2023

Types of inheritance in Entity Framework #TPH # TPT #TPC #ENTITY #ENTITYFRAMEWORK

 #TPH # TPT #TPC #ENTITY #ENTITYFRAMEWORK

  1. Table-per-Hierarchy (TPH)

    The TPH inheritance states that all entities, in a hierarchy of entities, are mapped to a single table in storage schema. It means, there is only one table in database and different Entity types in Entity model that inherits from a base Entity are mapped to that table.

    Table-per-Hierarchy (TPH)

    C# Implementation Code for TPH

     public class Course
     {
     [Key]
     public int CourseId { get; set; }
     public string Name { get; set; }
     public string Details { get; set; }
     public string Trainer { get; set; }
     }
    
     public class OnlineCourse : Course
     {
     public string URL { get; set; }
     }
    
     public class OfflineCourse : Course
     {
     public string Address { get; set; }
     }
    

    Entity Framework Code First Mapping for TPH

    modelBuilder.Entity<Course>()
     .Map<OnlineCourse >(m => m.Requires("Type").HasValue("OnlineCourse "))
     .Map<OfflineCourse >(m => m.Requires("Type").HasValue("OfflineCourse "));
    


  2. Table-per-Concrete-Type (TPC)

    The TPC inheritance states that each concrete class (a class which can be instantiated) in the hierarchy of entities is mapped to a separate table in storage schema. It means, there is separate table in database to maintain data for each derived entity type.

    Table-per-Concrete-Type (TPC)

    C# Implementation Code for TPC

     public abstract class Course //abstract class
     {
     [Key]
     public int CourseId { get; set; }
     public string Name { get; set; }
     public string Details { get; set; }
     public string Trainer { get; set; }
     }
    
     public class OnlineCourse : Course //concrete class
     {
     public string URL { get; set; }
     }
    
     public class OfflineCourse : Course //concrete class
     {
     public string Address { get; set; }
     }
    

    Entity Framework Code First Mapping for TPC

    modelBuilder.Entity<OnlineCourse >().Map(m =>
    {
     m.MapInheritedProperties();
     m.ToTable("OnlineCourse ");
    });
    
    modelBuilder.Entity<OfflineCourse >().Map(m =>
    {
     m.MapInheritedProperties();
     m.ToTable("OfflineCourse ");
    });
    

    Note

    The TPC inheritance is commonly used to inherit basic features.

  3. Table-per-Type (TPT)

    The TPT inheritance states that each entity in the hierarchy of entities is mapped to a separate table in storage schema. It means, there is separate table in database to maintain data for each Entity Type.

    Table-per-Type (TPT)

    C# Implementation Code for TPT

     public class Course
     {
     [Key]
     public int CourseId { get; set; }
     public string Name { get; set; }
     public string Details { get; set; }
     public string Trainer { get; set; }
     }
    
     public class OnlineCourse : Course
     {
     public string URL { get; set; }
     }
    
     public class OfflineCourse : Course
     {
     public string Address { get; set; }
     }
    

    Entity Framework Code First Mapping for TPT

    modelBuilder.Entity<Course>().ToTable("Course");
    modelBuilder.Entity<OnlineCourse >().ToTable("OnlineCourse ");
    modelBuilder.Entity<OfflineCourse >().ToTable("OfflineCourse ");

Use of Interface in c#

 Interfaces are an important feature of C# and other object-oriented programming languages, as they provide a way to define a contract for classes that implement them. Here are some of the reasons why interfaces are necessary in C#:

  1. Abstraction: Interfaces provide a way to abstract away implementation details and focus on the functionality that a class provides. By defining a set of methods and properties that a class must implement, interfaces enable you to create loosely coupled code that is easier to maintain and test.

  2. Polymorphism: Interfaces enable polymorphism, which means that different classes can implement the same interface and be used interchangeably in code that expects an object of that interface type. This allows for greater flexibility and extensibility in your code.

  3. Separation of Concerns: Interfaces enable you to separate concerns and create modular, reusable code. By defining interfaces that encapsulate specific functionality, you can create classes that implement those interfaces and use them in different parts of your application.

  4. Mocking: Interfaces are also useful for unit testing, as they allow you to easily create mock objects that implement the same interface as the object being tested. This makes it easier to isolate the code being tested and verify that it is working correctly.

Overall, interfaces are a powerful tool in C# that enable you to write cleaner, more modular, and more flexible code that is easier to maintain and test. They help to enforce good coding practices and can lead to more robust and reliable software.


#interface #oops #c# #.net #net #dotnet #programming #code #coding

Mastering Angular Reactive Forms: Part 1 - Building Dynamic and Interactive Forms

  #AngularReactiveForms #AngularForms #ReactiveFormsTutorial #AngularTutorial #AngularFormsPart1 #AngularFormValidation #AngularFormSu...