monstertjie_za
monstertjie_za

Reputation: 7803

Many-Many and One-Many on same collection Entity Framework 6 with InverseProeprty

I have the following:

public class Event : IEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [JsonProperty("id")]
    public Guid Id { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("date")]
    public DateTimeOffset Date { get; set; }

    [JsonProperty("distance")]
    public int Distance { get; set; }

    [JsonProperty("verticalAscend")]
    public int VerticalAscend { get; set; }

    [ForeignKey("User")]
    [JsonProperty("userId")]
    public Guid UserId { get; set; }

    //attending
    public DateTimeOffset DateCreated { get; set; }
    public DateTimeOffset DateModified { get; set; }

    [JsonProperty("user")]
    public virtual User User { get; set; }
    [JsonProperty("comments")]
    public virtual ICollection<Comment> Comments { get; set; }
    [JsonProperty("attending")]
    public virtual ICollection<User> AttendingList { get; set; }
}

And:

public class User : IEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [JsonProperty("id")]
    public Guid Id { get; set; }

    [JsonProperty("profilePicUrl")]
    public string ProfilePicUrl { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("surname")]
    public string LastName { get; set; }

    [JsonProperty("email")]
    public string Email { get; set; }

    public DateTimeOffset DateCreated { get; set; }
    public DateTimeOffset DateModified { get; set; }

    public virtual ICollection<Event> AttendingEvents { get; set; }
    [InverseProperty("User")]
    public virtual ICollection<Event> Events { get; set; }
}

Relationships:

Event:

User:

There exists a many-many relationship between the Event.AttendingList and User.AttendingEvents. There exists 0-many relationship between Event.User and User.Events, with ForeignKey as UserId.

I am trying to configure these with Fluent API, and using the InverseProperty to configure the other side of the relationship, mapping back to Event.User, but getting the following error:

Introducing FOREIGN KEY constraint 'FK_dbo.UserEvents_dbo.Events_Event_Id' on table 'UserEvents' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

I am unsure on how to solve this relationship on one table. What am I doing wrong?

Upvotes: 1

Views: 38

Answers (1)

TanvirArjel
TanvirArjel

Reputation: 32069

In the DbContext configure your models as follows:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     base.OnModelCreating(modelBuilder);

     modelBuilder.Entity<Event>().HasRequired(e => e.User)
            .WithMany(u => u.Events)
            .HasForeignKey(e => e.UserId)
            .WillCascadeOnDelete(false);

     modelBuilder.Entity<User>()
            .HasMany<Event>(s => s.AttendingEvents)
            .WithMany(c => c.AttendingList)
            .Map(cs =>
            {
                cs.MapLeftKey("UserId");
                cs.MapRightKey("EventId");
                cs.ToTable("UserEvents");
            });
}

Upvotes: 1

Related Questions