EF Code First foreign key with multiple keys

Dominick Piganell

I have an event table that can have multiple event dates for it. I've done some research into this error, but it looks like I have everything set up correctly, however I'm new to the code first approach, and I think my confusion comes in with overriding the OnModelCreating

One or more validation errors were detected during model generation:

Event_EventDates_Source_Event_EventDates_Target: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.



[Table("EventDates")]
public class EventDate
{
    [Key, Column(Order=1)]
    public int EventDateId { get; set; }
    [Key, Column(Order = 2)]
    public DateTime EventDateStart { get; set; }
    public DateTime? EventEnd { get; set; }
    public string TicketPurchaseUrl { get; set; }
}

[Table("Events")]
public class Event
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int EventId { get; set; }
    ...
    public int EventDateId { get; set; }
    public DateTime EventDateStart { get; set; }
    [ForeignKey("EventDateId, EventDateStart")]
    public virtual ICollection<EventDate> EventDates { get; set; }
}
Slauma

Your model is a bit weird for the business logic you describe. If an Event can have many EventDates you should not have a foreign key in Event that refers to EventDate, but the other way around: A foreign key in EventDate that refers to Event and possibly also a navigation property Event in EventDate:

[Table("EventDates")]
public class EventDate
{
    public int EventDateId { get; set; }

    public int EventId { get; set; }
    [ForeignKey("EventId")]
    public Event Event { get; set; }

    public DateTime EventDateStart { get; set; }
    public DateTime? EventEnd { get; set; }
    public string TicketPurchaseUrl { get; set; }
}

[Table("Events")]
public class Event
{
    public int EventId { get; set; }

    public virtual ICollection<EventDate> EventDates { get; set; }
}

By convention EventDate.EventDateId and Event.EventId will be the keys and they will be autogenerated. I suggest that you save yourself the trouble of having a DateTime as part of a composite key in EventDate. If you do not really need such a composite key keep it simple with just an int identity as key.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

EF Code First foreign key without navigation property

From Dev

EF Code First Single foreign key to multiple parents

From Dev

EF Code First Fluent API specifying the Foreign Key property

From Dev

Code-first Entity Framework - Multiple Foreign Keys For the Same Table

From Dev

Code First Multiple Foreign Keys for One to Many Relation

From Dev

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

From Dev

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

From Dev

Disable Foreign Key Constraint Code First EF

From Dev

EF6 Code First - Configure One-to-One relationship with unmatched foreign keys

From Dev

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

From Dev

Circular foreign key code first

From Dev

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

From Dev

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

From Dev

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

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

EF-Code First navigation property foreign key in complex type

From Dev

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

From Dev

Multiple Foreign Key for Same table in Entity Framework Code First

From Dev

Primary Key based on Foreign Keys EF

From Dev

EF Code First Migrations creating extra foreign key

From Dev

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

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 Code First The INSERT statement conflicted with the FOREIGN KEY constraint

From Dev

EF6 Code First - Configure One-to-One relationship with unmatched foreign keys

From Dev

Renaming foreign key properties in code first EF classes causes an exception

From Dev

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

From Dev

EF Code First Foreign Key Relationship

From Dev

Foreign key is created incorrrectly using EF code first

Related Related

  1. 1

    EF Code First foreign key without navigation property

  2. 2

    EF Code First Single foreign key to multiple parents

  3. 3

    EF Code First Fluent API specifying the Foreign Key property

  4. 4

    Code-first Entity Framework - Multiple Foreign Keys For the Same Table

  5. 5

    Code First Multiple Foreign Keys for One to Many Relation

  6. 6

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

  7. 7

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

  8. 8

    Disable Foreign Key Constraint Code First EF

  9. 9

    EF6 Code First - Configure One-to-One relationship with unmatched foreign keys

  10. 10

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

  11. 11

    Circular foreign key code first

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

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

  16. 16

    EF-Code First navigation property foreign key in complex type

  17. 17

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

  18. 18

    Multiple Foreign Key for Same table in Entity Framework Code First

  19. 19

    Primary Key based on Foreign Keys EF

  20. 20

    EF Code First Migrations creating extra foreign key

  21. 21

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

  22. 22

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

  23. 23

    EF Code First Foreign Key Same Table

  24. 24

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

  25. 25

    EF6 Code First - Configure One-to-One relationship with unmatched foreign keys

  26. 26

    Renaming foreign key properties in code first EF classes causes an exception

  27. 27

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

  28. 28

    EF Code First Foreign Key Relationship

  29. 29

    Foreign key is created incorrrectly using EF code first

HotTag

Archive