Foreign Key with Fluent API - Code First

GeekzSG

I am trying to establish foreign key to 2 classes using FluentAPI and bit confused on the way to implement it.

ApplicationUser uses ASP.NET Identity model and has UserId as string

public class ApplicationUser : IdentityUser
{
     public virtual List<UserProduct> Orders { get; set; }
}

Product that has a composite key on columns ProductID and ProductCategoryID

public class Product 
{
    public int ProductID { get; set; }
    public string ProductCategoryID { get; set; }

    public virtual List<UserProduct> Orders { get; set; }

    ...
}

and another class UserProduct that will have many-to-many relationship between ApplicationUser and Product table

public partial class UserProduct
{
    public string UserId { get; set; }

    public int ProductID { get; set; }
    public string ProductCategoryID { get; set; }

    public virtual ApplicationUser User { get; set; }

    public virtual Product Product { get; set; }
}

The FluentAPI code looks like

 modelBuilder.Entity<Product>().Property(t => t.ProductID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
 modelBuilder.Entity<Product>().HasKey(x => new { x.ProductID, x.ProductCategoryID  });

 modelBuilder.Entity<UserProduct>().HasKey(x => new {x.UserId, x.ProductID, x.ProductCategoryID});

How do I establish the foreign key relationship of UserProduct with ApplicationUser and Product?

Anton K

You can add the id-property (e.g. OrderId) to the class UserProduct and use this code to connect entities by foreign key.

modelBuilder.Entity<UserProduct>()
            .HasRequired(x => x.User) 
            .WithMany(u => u.Orders) 
            .HasForeignKey(x => x.OrderId);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

EF Code First Fluent API specifying the Foreign Key property

From Dev

Code first foreign key association MVC Entity

From Dev

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

From Dev

EF Foreign Key using Fluent API

From Dev

Disable Foreign Key Constraint Code First EF

From Dev

Advantage of defining relationships in Code First Fluent API?

From Dev

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

From Dev

Table only with foreign key in entity framework 6 (Fluent Api)

From Dev

EF Code First Fluent API - Cascade Delete

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Circular foreign key code first

From Dev

How to EF code first fluent API string primary key?

From Dev

Navigation property without foreign key, or fluent api version?

From Dev

entity framework code first fluent api

From Dev

EntityFramework foreign key as primary key with fluent API

From Dev

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

From Dev

EF Code First Foreign Key Same Table

From Dev

EF Foreign Key using Fluent API

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework one to optional foreign key code first fluent mapping

From Dev

EF Code First foreign key with multiple keys

From Dev

Table only with foreign key in entity framework 6 (Fluent Api)

From Dev

Foreign Key in Code First Entity Framework

From Dev

Expose Foreign key through fluent API

From Dev

Navigation property without foreign key, or fluent api version?

From Dev

Entity Framework: Foreign Key in code first

From Dev

Code first approach not creating foreign key

From Dev

Foreign keys with Code First in a Web API

From Dev

EF Code First Foreign Key Relationship

Related Related

  1. 1

    EF Code First Fluent API specifying the Foreign Key property

  2. 2

    Code first foreign key association MVC Entity

  3. 3

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

  4. 4

    EF Foreign Key using Fluent API

  5. 5

    Disable Foreign Key Constraint Code First EF

  6. 6

    Advantage of defining relationships in Code First Fluent API?

  7. 7

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

  8. 8

    Table only with foreign key in entity framework 6 (Fluent Api)

  9. 9

    EF Code First Fluent API - Cascade Delete

  10. 10

    Entity Framework Code First Foreign Key issue

  11. 11

    Circular foreign key code first

  12. 12

    How to EF code first fluent API string primary key?

  13. 13

    Navigation property without foreign key, or fluent api version?

  14. 14

    entity framework code first fluent api

  15. 15

    EntityFramework foreign key as primary key with fluent API

  16. 16

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

  17. 17

    EF Code First Foreign Key Same Table

  18. 18

    EF Foreign Key using Fluent API

  19. 19

    Entity Framework Code First Foreign Key issue

  20. 20

    Entity Framework one to optional foreign key code first fluent mapping

  21. 21

    EF Code First foreign key with multiple keys

  22. 22

    Table only with foreign key in entity framework 6 (Fluent Api)

  23. 23

    Foreign Key in Code First Entity Framework

  24. 24

    Expose Foreign key through fluent API

  25. 25

    Navigation property without foreign key, or fluent api version?

  26. 26

    Entity Framework: Foreign Key in code first

  27. 27

    Code first approach not creating foreign key

  28. 28

    Foreign keys with Code First in a Web API

  29. 29

    EF Code First Foreign Key Relationship

HotTag

Archive