Wednesday, 19 November 2025

EFcore best practice

 

🌉 What is a Navigation Property?

Think of navigation properties as shortcuts or links between two related tables in Entity Framework.

It’s like saying:

“Hey EF… this row belongs to that row — help me jump between them automatically.”


📌 Real-world example (your case)

You have:

1. TblUarRecord (Parent table)

Each UAR record can have many ReviewActions.

2. TblReviewAction (Child table)

Each ReviewAction belongs to one UAR record.

This is a one-to-many relationship.


🔗 So navigation properties help you do things like:

Instead of writing SQL manually:

SELECT * FROM tbl_ReviewAction WHERE UARRecordId = 1

You can simply do:

var actions = uarRecord.ReviewActions;

Or:

var uar = reviewAction.UarRecord;

That's the beauty of navigation properties — EF handles relationships for you.


🧩 How these properties look in your models

Parent Model (TblUarRecord)

public class TblUarRecord { public int UARRecordId { get; set; } // Navigation → One record has many review actions public ICollection<TblReviewAction> ReviewActions { get; set; } }

Child Model (TblReviewAction)

public class TblReviewAction { public int ReviewActionId { get; set; } public int UARRecordId { get; set; } // Navigation → This action belongs to one UAR record public TblUarRecord UarRecord { get; set; } }

🤯 Why this is useful?

1. Auto-joins

EF Core automatically joins tables when needed.

2. You can load related data easily

var record = await _context.TblUarRecord .Include(r => r.ReviewActions) .FirstAsync(r => r.UARRecordId == 1);

3. Cascade operations

Delete a UARRecord → EF can delete its actions automatically (if enabled).

4. Cleaner code

You work with objects, not SQL.


🎯 Quick Summary

Navigation Property = shortcut to related table

  • Helps EF understand relationships

  • Enables .Include(), lazy loading, etc.

  • You can access related data easily

  • Useful in parent–child tables (like yours)


If you want, I can update your full models with proper navigation properties + foreign key attributes.

No comments:

Post a Comment

HCL healthcare

 hcl health care f2f second round interview asked more uestions on microservice 1.apim manage all apis in one place 2.sql profiler in one fo...