Entity framework code first, get access to foreign key value

Hay Zohar

Say we have a project with this entities:

class User
{
    public Guid Id { get; set; }
    public List<Message> Messages { get; set; }
    ...
}

class Message
{
    public Guid Id { get; set; }
    public string Message { get; set; }
}

Now consider a scenerio where i want to get all the messages that a certain user posted, how can one achieve this without pulling the user information aswell? (without using the context.Users.Include(...) ) ? I know that the entity framework creates a column in the Message table that holds the Id of the user that posted this message, but how can i have an access to this value? as it is not a property in my original class.

DavidG

You can add a navigation property into the Message class:

public class Message
{
    public Guid Id { get; set; }
    public string Message { get; set; }

    public virtual User User { get; set; }
}

And then query your context like this:

var userMessages = context.Messages
    .Where(m => m.User.Id == 5);

This is the tidier way of doing it. Alternatively, you could start with the user, but this is a little more awkward:

var userMessages = context.Users
    .Where(u => u.Id == 5)
    .SelectMany(u => u.Messages);

Both methods will ultimately produce the similar SQL, something like this:

SELECT [Extent1].[Column1],
       [Extent1].[Column2],
       [Extent1].[Column3]
FROM [dbo].[Messages] AS [Extent1] 
WHERE 5 = [Extent1].[UserId]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get foreign key value using Entity Framework code first

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Foreign Key in Code First Entity Framework

From Dev

Entity Framework: Foreign Key in code first

From Dev

Defining multiple Foreign Key for the Same table in Entity Framework Code First

From Dev

Entity Framework Code First Foreign Key adding Index as well

From Dev

Entity Framework 6 Code First Foreign Key Without Corresponding Properties

From Dev

Entity Framework Code First and Firebird - Foreign Key name issue

From Dev

Entity Framework Code first adds unwanted foreign key column

From Dev

Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

From Dev

Multiple Foreign Key for Same table in Entity Framework Code First

From Dev

Entity Framework one to optional foreign key code first fluent mapping

From Dev

Entity framework code first cant create primary and foreign key relationship

From Dev

Entity framework code first, custom foreign key name for inheritance

From Dev

How to specify a foreign key with code-first Entity Framework

From Dev

Exchanged Foreign Key on Entity Framework Code First From data base

From Dev

Code First entity framework and foreign keys

From Dev

Entity Framework Code First initializing foreign keys

From Dev

Code first foreign key association MVC Entity

From Dev

Include foreign key in composite primary key in Code-First Entity Framework

From Dev

How to get parent entity property in child entity with foreign key relation in code first?

From Dev

How to get parent entity property in child entity with foreign key relation in code first?

From Dev

How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

From Dev

Entity Framework 6 multiple table to one foreign key relationship code first

From Dev

Entity Framework, Code First: How can i make a Foreign Key Not-Nullable

From Dev

Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First

From Dev

Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework

From Dev

Entity framework 6 code first saving collection via foreign key relation

Related Related

  1. 1

    Get foreign key value using Entity Framework code first

  2. 2

    Entity Framework Code First Foreign Key issue

  3. 3

    Entity Framework Code First Foreign Key issue

  4. 4

    Foreign Key in Code First Entity Framework

  5. 5

    Entity Framework: Foreign Key in code first

  6. 6

    Defining multiple Foreign Key for the Same table in Entity Framework Code First

  7. 7

    Entity Framework Code First Foreign Key adding Index as well

  8. 8

    Entity Framework 6 Code First Foreign Key Without Corresponding Properties

  9. 9

    Entity Framework Code First and Firebird - Foreign Key name issue

  10. 10

    Entity Framework Code first adds unwanted foreign key column

  11. 11

    Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

  12. 12

    Multiple Foreign Key for Same table in Entity Framework Code First

  13. 13

    Entity Framework one to optional foreign key code first fluent mapping

  14. 14

    Entity framework code first cant create primary and foreign key relationship

  15. 15

    Entity framework code first, custom foreign key name for inheritance

  16. 16

    How to specify a foreign key with code-first Entity Framework

  17. 17

    Exchanged Foreign Key on Entity Framework Code First From data base

  18. 18

    Code First entity framework and foreign keys

  19. 19

    Entity Framework Code First initializing foreign keys

  20. 20

    Code first foreign key association MVC Entity

  21. 21

    Include foreign key in composite primary key in Code-First Entity Framework

  22. 22

    How to get parent entity property in child entity with foreign key relation in code first?

  23. 23

    How to get parent entity property in child entity with foreign key relation in code first?

  24. 24

    How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

  25. 25

    Entity Framework 6 multiple table to one foreign key relationship code first

  26. 26

    Entity Framework, Code First: How can i make a Foreign Key Not-Nullable

  27. 27

    Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First

  28. 28

    Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework

  29. 29

    Entity framework 6 code first saving collection via foreign key relation

HotTag

Archive