Creating Relational Tables using EF Core
IHUB Talent – The Best Full Stack .NET Training Institute in Hyderabad with Live Internship Program
If you're planning to build a rewarding career in software development, then IHUB is the perfect place to begin. Recognized as the best Full Stack .NET training institute in Hyderabad, IHUB offers a complete job-oriented course with a live intensive internship program, guided by real-time industry experts.
Whether you're a graduate, postgraduate, someone with an educational gap, or planning a job domain change, IHUB’s Full Stack .NET course is designed to make you job-ready in just a few months. The program is structured for freshers and working professionals alike, offering hands-on experience and placement support.
🔹 Why Choose IHUB for Full Stack .NET Training?
Expert Faculty: Learn from certified trainers with real-world industry experience.
Live Intensive Internship: Work on actual projects under the guidance of professionals.
Placement Assistance: Resume preparation, mock interviews, and job referrals.
Suitable for All Backgrounds: Freshers, students with gaps, or those changing job domains.
Flexible Learning: Online and offline training options available.
Project-Based Training: Gain hands-on experience by developing end-to-end real-time projects.
Creating Relational Tables Using EF Core
Entity Framework Core (EF Core) is a powerful ORM (Object-Relational Mapper) that simplifies database interactions in .NET applications. One of its core strengths is enabling developers to define and manage relational tables directly from their C# classes using a feature called Code First.
Let’s walk through how to model relational data—say, a one-to-many relationship between Author and Book.
1. Define Your Entities
csharp
public class Author
{
public int AuthorId { get; set; }
public string Name { get; set; }
public List<Book> Books { get; set; } = new();
}
public class Book
{
public int BookId { get; set; }
public string Title { get; set; }
public int AuthorId { get; set; }
public Author Author { get; set; }
}
Here, each Author can have many Books, and each Book is linked to one Author via a foreign key.
2. Configure dB Context
csharp
public class AppDbContext : DbContext
{
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer("YourConnectionString");
}
3. Migrations & Database Creation
Run the following commands:
bash
dotnet ef migrations add InitialCreate
dotnet ef database update
EF Core will generate and apply the necessary SQL to create the relational tables.
Final Thoughts
EF Core makes relational modeling intuitive and maintainable by keeping your C# code in sync with your database structure. Whether you're working with one-to-many, many-to-many, or one-to-one relationships, EF Core offers a clean, code-first approach.
Read More
Code First vs Database First in Entity Framework
Database Migrations using Entity Framework Core
How to Connect ASP.NET Core with SQL Server
Repository Pattern in ASP.NET Core
Working with JSON in .NET Web APIs
Visit Our I-HUB Talent Training Institute Hyderabad
Comments
Post a Comment