How to define a table that its primary key is constructed from 2 foreign keys with EF code-first

lobengula3rd

I am using models to define my tables using EF code-first.

I have and Item model and an Order model.

Item:

 public class Item
    {
        public int ItemID { get; set; }

        [Required]
        public int Price { get; set; }

        [Required]
        public int AmountLeft { get; set; }

        [Required]
        public string Image { get; set; }

        [Required]
        public string Description { get; set; }

        [Required]
        public string FullDescription { get; set; }

        [Required]
        public DateTime PublishDate { get; set; }


        public int CompanyID { get; set; }
        public int CategoryID { get; set; }

        // Navigation properties 
        public virtual Company Company { get; set; }

        // Navigation properties 
        public virtual Category Category { get; set; } 

    }

Order model:

public class Order
    {
        public int OrderID { get; set; }

        [Required]
        public DateTime DeliveryDate { get; set; }

        [Required]
        public string Currency { get; set; }

        [Required]
        public int TotalAmount { get; set; }

        public List<int> Items { get; set; }


        public DateTime OrderDate { get; set; }


        public int UserID { get; set; }
        // Navigation properties 
        public virtual User user { get; set; }

    }

I want to create another table which will be called ItemInOrder which will only have 2 fields: ItemID and OrderID. the primary key would be these 2 foreign keys.

i tried to define this model:

   public class ItemInOrder
    {

        public int OrderID { get; set; }
        public int ItemID { get; set; }

        // Navigation properties 
        public virtual Order order { get; set; }
        public virtual Item item { get; set; }

    }

But i got errors. i tried to put [Key] notation on both fields but still i got errors.

how will i be able to create the table i want?

octavioccl

When you need to create a table with composite PKs, you need to specify the order of you keys. There are two variants:

You could override the OnModelCreating method on your Context, and try with these Fluent Api configurations:

// Configure the primary keys for the ItemInOrder in the order you want
modelBuilder.Entity<ItemInOrder>() 
    .HasKey(t => new{t.OrderID,ItemID); 

modelBuilder.Entity<ItemInOrder>() 
            .HasRequired(io=>io.Order)
            .WithMany()
            .HasForeigKey(io=>io.OrderID);

 modelBuilder.Entity<ItemInOrder>() 
           .HasRequired(io=>io.Item)
           .WithMany()
           .HasForeigKey(io=>io.ItemID);

The second variant using Data Annotations should be this way:

[Key] 
[Column(Order=1)] 
[ForeignKey("Order")]
public int OrderID { get; set; }

[Key] 
[Column(Order=2)] 
[ForeignKey("Item")]
public int ItemID { get; set; }

EF will notice you want to create two relationships and it will do the job for you.

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 foreign key with multiple keys

From Dev

EF Code First - mapping two foreign key columns in child table to the same primary key

From Dev

EF Code First Foreign Key Same Table

From Dev

SQL Server : multiple foreign keys from first table linking to the primary key of second table

From Dev

SQL Server : multiple foreign keys from first table linking to the primary key of second table

From Dev

Primary Key based on Foreign Keys EF

From Dev

MVC modelbuilding: multiple foreign keys in table, code first EF

From Dev

MVC modelbuilding: multiple foreign keys in table, code first EF

From Dev

creating table with primary key and 2 foreign keys with error

From Dev

Foreign Key Coming from 2 Primary Keys from Distinct Tables

From Dev

Table with 2 foreign keys coming up from one primary

From Dev

Multiplicity is not valid in Role in relationship: EF code first one to one relationship with same primary key and foreign key

From Dev

Laravel : How do I get records from pivot table whereby its foreign key does not reference a primary key?

From Dev

How to EF code first fluent API string primary key?

From Dev

How to assign primary key values into foreign keys?

From Dev

Delete data from table with foreign keys as primary

From Dev

Do primary key constraints get applied to foreign keys by convention in EntityFramework Code First

From Dev

Code First - Two foreign keys as primary keys, unable to add migration

From Dev

Disable Foreign Key Constraint Code First EF

From Dev

EF Code First Foreign Key Relationship

From Dev

Can a foreign key refer to the primary key of its own table?

From Dev

Remove table name prefix in EF code first foreign key table column

From Dev

How to refer primary key as Foreign to various table

From Dev

mysql Multiple Foreign Keys in a Table to the Same Primary Key

From Dev

Create table with both foreign keys and a composite primary key

From Dev

EF Code First: Add row to table with a non-identity primary key

From Dev

How to insert primary key from one table as a foreign key into secondary table

From Dev

How to add a primary (Surrogate) key from one table into a foreign key of another table?

From Dev

How to insert primary key from one table as a foreign key into secondary table

Related Related

  1. 1

    EF Code First foreign key with multiple keys

  2. 2

    EF Code First - mapping two foreign key columns in child table to the same primary key

  3. 3

    EF Code First Foreign Key Same Table

  4. 4

    SQL Server : multiple foreign keys from first table linking to the primary key of second table

  5. 5

    SQL Server : multiple foreign keys from first table linking to the primary key of second table

  6. 6

    Primary Key based on Foreign Keys EF

  7. 7

    MVC modelbuilding: multiple foreign keys in table, code first EF

  8. 8

    MVC modelbuilding: multiple foreign keys in table, code first EF

  9. 9

    creating table with primary key and 2 foreign keys with error

  10. 10

    Foreign Key Coming from 2 Primary Keys from Distinct Tables

  11. 11

    Table with 2 foreign keys coming up from one primary

  12. 12

    Multiplicity is not valid in Role in relationship: EF code first one to one relationship with same primary key and foreign key

  13. 13

    Laravel : How do I get records from pivot table whereby its foreign key does not reference a primary key?

  14. 14

    How to EF code first fluent API string primary key?

  15. 15

    How to assign primary key values into foreign keys?

  16. 16

    Delete data from table with foreign keys as primary

  17. 17

    Do primary key constraints get applied to foreign keys by convention in EntityFramework Code First

  18. 18

    Code First - Two foreign keys as primary keys, unable to add migration

  19. 19

    Disable Foreign Key Constraint Code First EF

  20. 20

    EF Code First Foreign Key Relationship

  21. 21

    Can a foreign key refer to the primary key of its own table?

  22. 22

    Remove table name prefix in EF code first foreign key table column

  23. 23

    How to refer primary key as Foreign to various table

  24. 24

    mysql Multiple Foreign Keys in a Table to the Same Primary Key

  25. 25

    Create table with both foreign keys and a composite primary key

  26. 26

    EF Code First: Add row to table with a non-identity primary key

  27. 27

    How to insert primary key from one table as a foreign key into secondary table

  28. 28

    How to add a primary (Surrogate) key from one table into a foreign key of another table?

  29. 29

    How to insert primary key from one table as a foreign key into secondary table

HotTag

Archive