EF Code First Fluent API specifying the Foreign Key property

Richard Pawson

I have a class AgentBalance with an association to Agent, thus:

public class AgentBalance
{
    ...

    public int AgentId { get; set; }

    public virtual Agent Agent { get; set; }

}

AgentId is detected as the FK for the Agent relationship by convention, but I want to make it explicit in the Mapping class, to be safer against future changes. If Agent had a collection of Balances then I know how to do this e.g.:

HasRequired(t => t.Agent).WithMany(a => a.Balances).HasForeignKey(t => t.AgentId);

However, Agent does not have a collection of Balances - I don't want that association to be reverse navigable. But without the .WithMany in the mapping I don't get the option to specify .HasForeignKey. Is there another way? (N.B. I know I could also do this using attributes, but I want to use the fluent API mapping).

SOfanatic

I believe you should be able to do this:

HasRequired(t => t.Agent).WithMany().HasForeignKey(t => t.AgentId)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Foreign Key with Fluent API - Code First

From Dev

EF6 Code First: Using Fluent API to declare a Foreign Key

From Dev

How to EF code first fluent API string primary key?

From Dev

EF Foreign Key using Fluent API

From Dev

EF Foreign Key using Fluent API

From Dev

EF Code First Approach: Confused in EF Foreign Key constraint by fluent syntax

From Java

EF Code First foreign key without navigation property

From Dev

EF-Code First navigation property foreign key in complex type

From Dev

EF Code First Fluent API - Cascade Delete

From Dev

How to expose Foreign Key property to existing entity having navigational property using EF6 Code First

From Dev

Disable Foreign Key Constraint Code First EF

From Dev

EF Code First Foreign Key Same Table

From Dev

EF Code First foreign key with multiple keys

From Dev

EF Code First Foreign Key Relationship

From Dev

Entity Framework one to optional foreign key code first fluent mapping

From Dev

Define Foreign Key with Fluent API for entity with no navigation property

From Dev

Navigation property without foreign key, or fluent api version?

From Dev

Navigation property without foreign key, or fluent api version?

From Dev

Issue coming in constraint using Fluent API EF Code First

From Dev

EF5 Code First - Data Annotations vs Fluent API

From Dev

Getting Class instance in Collection - EF Code First Fluent API

From Dev

EF Code First self rating by DataAnnotations or fluent API

From Dev

EF Composite key fluent API

From Dev

EF Composite key fluent API

From Dev

EF Code First The INSERT statement conflicted with the FOREIGN KEY constraint

From Dev

EF Code First Single foreign key to multiple parents

From Dev

EF Code First Migrations creating extra foreign key

From Dev

EF code first foreign key ignored, getting "Invalid column name"

From Dev

EF Code First The INSERT statement conflicted with the FOREIGN KEY constraint

Related Related

  1. 1

    Foreign Key with Fluent API - Code First

  2. 2

    EF6 Code First: Using Fluent API to declare a Foreign Key

  3. 3

    How to EF code first fluent API string primary key?

  4. 4

    EF Foreign Key using Fluent API

  5. 5

    EF Foreign Key using Fluent API

  6. 6

    EF Code First Approach: Confused in EF Foreign Key constraint by fluent syntax

  7. 7

    EF Code First foreign key without navigation property

  8. 8

    EF-Code First navigation property foreign key in complex type

  9. 9

    EF Code First Fluent API - Cascade Delete

  10. 10

    How to expose Foreign Key property to existing entity having navigational property using EF6 Code First

  11. 11

    Disable Foreign Key Constraint Code First EF

  12. 12

    EF Code First Foreign Key Same Table

  13. 13

    EF Code First foreign key with multiple keys

  14. 14

    EF Code First Foreign Key Relationship

  15. 15

    Entity Framework one to optional foreign key code first fluent mapping

  16. 16

    Define Foreign Key with Fluent API for entity with no navigation property

  17. 17

    Navigation property without foreign key, or fluent api version?

  18. 18

    Navigation property without foreign key, or fluent api version?

  19. 19

    Issue coming in constraint using Fluent API EF Code First

  20. 20

    EF5 Code First - Data Annotations vs Fluent API

  21. 21

    Getting Class instance in Collection - EF Code First Fluent API

  22. 22

    EF Code First self rating by DataAnnotations or fluent API

  23. 23

    EF Composite key fluent API

  24. 24

    EF Composite key fluent API

  25. 25

    EF Code First The INSERT statement conflicted with the FOREIGN KEY constraint

  26. 26

    EF Code First Single foreign key to multiple parents

  27. 27

    EF Code First Migrations creating extra foreign key

  28. 28

    EF code first foreign key ignored, getting "Invalid column name"

  29. 29

    EF Code First The INSERT statement conflicted with the FOREIGN KEY constraint

HotTag

Archive