Entity framework many-to-many relationship

user2915962

Ok, I'm trying to figure out how to setup my DB properly.

I have two classes:

public class Event
{
    public int EventId { get; set; }
    public string EventName { get; set; }
}

and

public class Dog
{
    public int DogId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

An event must be able to contain a collection of dogs. Every dog must be able to be part in any event. I'm guessing this is what they call a many-to-many relationship, but I do not now how to set it up with keys etc. I hope I'm clear enough with what I'm hoping to achieve.

I asked a similar question yesterday but I was then not clear over what I needed: Have a list of objects as a foreign key.

Thanks!

StuartLC

Yes, that is a N:N relationship. Assuming Code First, change your entities:

public class Event
{
    public Event()
    {
       Dogs = new HashSet<Dog>();
    }
    public int EventId { get; set; }
    public string EventName { get; set; }
    public virtual ICollection<Dog> Dogs { get; set; }
}

public class Dog
{
    public Dog()
    {
       Events = new HashSet<Event>();
    }
    public int DogId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public virtual ICollection<Event> Events { get; set; }
}

And your OnModelCreating:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
  modelBuilder.Entity<Dog>()
    .HasMany(d => d.Events)
    .WithMany(e => e.Dogs)
    .Map(m =>
    {
       m.MapLeftKey("DogId");
       m.MapRightKey("EventId");
       m.ToTable("DogEvent");
    });
}

This should then create a junction table DogEvent with just the two foreign keys, DogId and EventId

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Many to many relationship in Entity Framework

From Dev

Entity Framework: One to Many relationship

From Dev

Entity Framework many to many relationship Delete

From Dev

Saving Many to Many relationship - Entity Framework

From Dev

Entity Framework specify cardinality in a many to many relationship

From Dev

How many to many relationship in Entity Framework is working

From Dev

Update a Many to Many relationship using Entity Framework?

From Dev

Entity Framework duplicate entries in many to many relationship

From Dev

Updating many-to-many relationship entity framework

From Dev

Entity Framework Many TO Many Relationship with Primary Key

From Dev

Entity Framework with many to many relationship generetad tables

From Dev

Entity Framework Core Creating a Many to Many Relationship

From Dev

Entity Framework one-to-many and many-to-many relationship

From Dev

Entity Framework one-to-many and many-to-many relationship

From Dev

Eager Loading Many to Many relationship with an Association Entity - Entity Framework

From Dev

Entity Framework 4.2 One to many relationship

From Dev

One to many relationship in Entity Framework 6

From Dev

Entity Framework One-to-Many relationship with ordering

From Dev

Entity Framework - Trouble with one-to-many relationship

From Dev

entity framework 5, delete one to many relationship?

From Dev

Exception with Entity Framework 6, disconnected context and many-to-many relationship

From Dev

How to use Entity Framework 6 to update many-to-many relationship?

From Dev

Keep list of foreign keys in many-to-many Entity Framework relationship

From Dev

Mapping many to many relationship in entity framework code first

From Dev

Entity Framework not writing many to many relationship changes to the database

From Dev

Entity Framework "WHERE IN" style for Many-To-Many Relationship

From Dev

Query a many-to-many relationship with linq/Entity Framework. CodeFirst

From Dev

Add data to a many-to-many relationship using Entity Framework

From Dev

Entity Framework Delete Many to Many Relationship using stubs

Related Related

  1. 1

    Many to many relationship in Entity Framework

  2. 2

    Entity Framework: One to Many relationship

  3. 3

    Entity Framework many to many relationship Delete

  4. 4

    Saving Many to Many relationship - Entity Framework

  5. 5

    Entity Framework specify cardinality in a many to many relationship

  6. 6

    How many to many relationship in Entity Framework is working

  7. 7

    Update a Many to Many relationship using Entity Framework?

  8. 8

    Entity Framework duplicate entries in many to many relationship

  9. 9

    Updating many-to-many relationship entity framework

  10. 10

    Entity Framework Many TO Many Relationship with Primary Key

  11. 11

    Entity Framework with many to many relationship generetad tables

  12. 12

    Entity Framework Core Creating a Many to Many Relationship

  13. 13

    Entity Framework one-to-many and many-to-many relationship

  14. 14

    Entity Framework one-to-many and many-to-many relationship

  15. 15

    Eager Loading Many to Many relationship with an Association Entity - Entity Framework

  16. 16

    Entity Framework 4.2 One to many relationship

  17. 17

    One to many relationship in Entity Framework 6

  18. 18

    Entity Framework One-to-Many relationship with ordering

  19. 19

    Entity Framework - Trouble with one-to-many relationship

  20. 20

    entity framework 5, delete one to many relationship?

  21. 21

    Exception with Entity Framework 6, disconnected context and many-to-many relationship

  22. 22

    How to use Entity Framework 6 to update many-to-many relationship?

  23. 23

    Keep list of foreign keys in many-to-many Entity Framework relationship

  24. 24

    Mapping many to many relationship in entity framework code first

  25. 25

    Entity Framework not writing many to many relationship changes to the database

  26. 26

    Entity Framework "WHERE IN" style for Many-To-Many Relationship

  27. 27

    Query a many-to-many relationship with linq/Entity Framework. CodeFirst

  28. 28

    Add data to a many-to-many relationship using Entity Framework

  29. 29

    Entity Framework Delete Many to Many Relationship using stubs

HotTag

Archive