Monday, 27 July 2020

C# - Relationships in Entity Framework Core


One To One Relationships

public class Author
{
    public int AuthorId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public AuthorBiography Biography { get; set; }
}

public class AuthorBiography
{
    public int AuthorBiographyId { get; set; }
    public string Biography { get; set; }
    public DateTime DateOfBirth { get; set; }
    public string PlaceOfBirth { get; set; }
    public string Nationality { get; set; }
   public int AuthorRef { get; set; }
    public Author Author { get; set; }
}
It is configured using the Fluent API like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
   modelBuilder.Entity<Author>()
       .HasOne(a => a.Biography)
       .WithOne(b => b.Author)
        .HasForeignKey<AuthorBiography>(b => b.AuthorRef);
}

 

One To Many Relationships

public class Company
{
   public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Employee> Employees { get; set; }
}

public class Employee
{
   public int Id { get; set; }
    public string Name { get; set; }
    public Company Company { get; set; }
}
A company has many employees, each with one company.
protected override void OnModelCreating(Modelbuilder modelBuilder)
{
    modelBuilder.Entity<Company>()
        .HasMany(c => c.Employees)
        .WithOne(e => e.Company);
}
It can also be configured by starting with the other end of the relationship:
protected override void OnModelCreating(Modelbuilder modelBuilder)
{
    modelBuilder.Entity<Employee>()
        .HasOne(e => e.Company)
        .WithMany(c => c.Employees);
}

Many To Many Relationships

A many-to-many relationship is defined in code by the inclusion of collection properties in each of the entities - The Categories property in the Book class, and the Books property in the Category class:

public class Book
{
    public int BookId { get; set; }
    public string Title { get; set; }
    public Author Author { get; set; }
    public ICollection<Category> Categories { get; set; }
}  


public class Category
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public ICollection<Book> Books { get; set; }
}
Entity Framework, this model definition was sufficient for EF to imply the correct type of relationship and to generate the join table for it. In EF Core it is necessary to include an entity in the model to represent the join table, and then add navigation properties to either side of the many-to-many relations that point to the join entity instead:
public class Book
{
    public int BookId { get; set; }
    public string Title { get; set; }
    public Author Author { get; set; }
    public ICollection<BookCategory> BookCategories { get; set; }
}  

public class Category
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public ICollection<BookCategory> BookCategories { get; set; }
}  

public class BookCategory
{
   public int BookId { get; set; }
    public Book Book { get; set; }
   public int CategoryId { get; set; }
    public Category Category { get; set; }
}
The join table will be named after the join entity (BookCategory in this case) by convention. The relationship also needs to be configured via the Fluent API for EF Core to be able to map it successfully:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<BookCategory>()
        .HasKey(bc => new { bc.BookId, bc.CategoryId });  

   modelBuilder.Entity<BookCategory>()
        .HasOne(bc => bc.Book)
        .WithMany(b => b.BookCategories)
        .HasForeignKey(bc => bc.BookId);  

   modelBuilder.Entity<BookCategory>()
        .HasOne(bc => bc.Category)
        .WithMany(c => c.BookCategories)
       .HasForeignKey(bc => bc.CategoryId);
}
The primary key for the join table is a composite key comprising both of the foreign key values. In addition, both sides of the many-to-many relationship are configured using the HasOne, WithMany and HasForeignKey Fluent API methods.
This is sufficient if you want to access book category data via the Book or Category entities. If you want to query BookCategory data directly, you should also add a DbSet for the join table to the context:
public class SampleContext : DbContext
{
    public DbSet<Book> Books { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<BookCategory> BookCategories { get; set; }
}


Wednesday, 15 July 2020

C# - Different Types Of Action Results In ASP.NET MVC


Types of Action Results
There are different Types of action results in ASP.NET MVC. Each result has a different type of result format to view page.
  1. View Result
  2. Partial View Result
  3. Redirect Result
  4. Redirect To Action Result
  5. Redirect To Route Result
  6. Json Result
  7. File Result
  8. Content Result

View Result
View result is a basic view result. It returns basic results to view page. View result can return data to view page through which class is defined in the model. View page is a simple HTML page.
public ViewResult About(){  
   ViewBag.Message = "Your application description page.";  
   return View();  
} 

Partial View Result
Partial View Result is returning the result to Partial view page. Partial view is one of the views that we can call inside Normal view page.
public PartialViewResult Index() {  
return PartialView("_PartialView");  
} 

Redirect Result
Redirect result is returning the result to specific URL. It is rendered to the page by URL. If it gives wrong URL, it will show 404 page errors.
public RedirectResult Index(){  
return Redirect("Home/Contact");  
}  

Redirect to Action Result
Redirect to Action result is returning the result to a specified controller and action method. Controller name is optional in Redirect to Action method. If not mentioned, Controller name redirects to a mentioned action method in current Controller. Suppose action name is not available but mentioned in the current controller, then it will show 404 page error.
public ActionResult Index () {  
return RedirectToAction("Login", "Account");  
}  

Json Result
Json result is a significant Action Result in MVC. It will return simple text file format and key value pairs. If we call action method, using Ajax, it should return Json result.

public ActionResult Index(){
var persons = new List<Person1> {
new Person1{Id=1, FirstName="Harry", LastName="Potter"},  
new Person1{Id=2, FirstName="James", LastName="Raj"} 
};  
return Json(persons, JsonRequestBehavior.AllowGet);
}  

File Result
File Result returns different file format view page when we implement file download concept in MVC using file result. 
public ActionResult Index () {  
return File("Web.Config", "text");  
}  

Content Result
Content result returns different content's format to view. MVC returns different format using content return like HTML format, Java Script format and any other format.

public ActionResult Contact(){  
ViewBag.Message = "Your contact page.";  
 return View();  
}