Foreign Key Mapping

|
| By Adarsh Thakur

In C#, foreign key mapping is a pivotal aspect when working with relational databases through Object-Relational Mapping (ORM) frameworks like Entity Framework. Foreign key mappings establish relationships between tables/entities, facilitating data integrity and enabling efficient data retrieval from the databases.

Here’s an example of how you can create a foreign key mapping using the Entity Framework:
Let’s say you have two entities: Author and Book. Each Book has an AuthorId, which is a foreign key referencing the Author entity. It’ll demonstrate the concept with basic classes representing a one-to-many relationship between Author and Book:

using System;
using System.Collections.Generic;

// Define the Author class
public class Author
{
public int AuthorId { get; set; }
public string Name { get; set; }

// A collection of books associated with an author
public List Books { get; set; }
}

// Define the Book class
public class Book
{
public int BookId { get; set; }
public string Title { get; set; }

// Foreign key property referring to the Author
public int AuthorId { get; set; }

// Navigation property to represent the Author of the book
public Author Author { get; set; }
}

class Program
{
static void Main(string[] args)
{
// Create an author
Author author = new Author
{
AuthorId = 1,
Name = "John Doe"
};

// Create books and associate them with the author
Book book1 = new Book
{
BookId = 1,
Title = "Book 1",
AuthorId = author.AuthorId, // Assign the author's ID to the book
Author = author // Set the Author navigation property
};
Book book2 = new Book
{
BookId = 2,
Title = "Book 2",
AuthorId = author.AuthorId, // Assign the author's ID to the book
Author = author // Set the Author navigation property
};

// Create a list of books for the author
author.Books = new List { book1, book2 };

// Display author and associated books
Console.WriteLine($"Author: {author.Name}");
foreach (var book in author.Books)
{
Console.WriteLine($"Book Title: {book.Title}");
}
}
}


The output for the above code will be:

Author: John Doe
Book Title: Book 1
Book Title: Book 2

In this example, there are two classes: “Author” and “Book”. An Author can have multiple Book instances associated with them. The “Author” class contains an AuthorId and a collection of Books.
The “Book” class includes a BookId, Title, and an AuthorId property representing the foreign key referencing the Author entity. Additionally, there’s an “Author” navigation property to represent the association between an author and their books.

The main method demonstrates the creation of an author and two books associated with that author. It then displays the author’s name along with the titles of the books associated with them.

This simple example demonstrates the concept of a foreign key relationship between “Author” and “Book” without using a specific database or ORM.

Foreign key mappings in C# are crucial for maintaining the relationships between entities in a relational database, ensuring data consistency, and enabling effective querying and manipulation of related data within an application.

Leave a Reply

Your email address will not be published. Required fields are marked *