Edit 1: So I wasn't getting anywhere and tried something different. I renamed my FriendRequest class to FriendRequestBase and made it abstract and then I made a FriendRequest class that inherits from FriendRequestBase without any extra fields, so now the inheritance looks like this: FriendRequest : FriendRequestBase GroupModRequest : FriendRequestBase

That should work, right? Well now, I added a migration to see if it will add any changes and yes, for some reason it now starts dropping foreign keys.

DropForeignKey("dbo.GroupModRequests", "SenderID", "dbo.AspNetUsers");
DropForeignKey("dbo.GroupModRequests", "ReceiverID", "dbo.AspNetUsers");

What could be the problem there? I've correctly set up the foreign keys with the Fluent API:

        modelBuilder.Entity<GroupModRequest>()
            .HasRequired(r => r.Receiver)
            .WithMany()
            .HasForeignKey(k => k.ReceiverID)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<GroupModRequest>()
            .HasRequired(r => r.Sender)
            .WithMany()
            .HasForeignKey(k => k.SenderID)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<GroupModRequest>()
            .HasRequired(r => r.Group)
            .WithMany(g => g.GroupModRequests)
            .HasForeignKey(k => k.GroupID)
            .WillCascadeOnDelete(true);

I'm trying to seed my database after running 'update-database' and this is what I'm getting:

All objects in the EntitySet 'ApplicationDbContext.FriendRequests' must have unique primary keys. However, an instance of type 'AW.Models.GroupModRequest' and an instance of type 'AW.Models.FriendRequest' both have the same primary key value, 'EntitySet=FriendRequests;SenderID=a9540bd6-8532-4c7a-9f68-19d2aeecffcb;ReceiverID=8f50eccf-8ccf-432e-a033-82d933b5e3f5'.

As I mentioned previously, FriendRequests are in one table and GroupModRequests are in a different table so this error doesn't make sense. I can kind of guess why this happens, though.

GroupModRequests inherits from FriendRequests and I'm using the Table-Per-Conrete-Class approach, meaning that FriendRequests has a table and GroupModRequests has a table and it has all of the properties FriendRequests have plus some more. Note, that FriendRequests isn't an abstract class.

FriendRequests has a composite primary key consisting of: SenderID, ReceiverID. GroupModRequests has a composite primary key consisting of: SenderID, ReceiverID and GroupID.

And so I bet that for some reason it is ignoring the 'GroupID' part of the key and ignoring the fact that they are in different tables and throws this error when a FriendRequest has the same Sender/Receiver ID as a GroupModRequest.

So how can I fix this in EF6? I already set this with fluent, but apparently that isn't enough:

modelBuilder.Entity<GroupModRequest>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("GroupModRequests");
            });

The composite keys look like this:

 public class FriendRequest
{
    [Key, Column(Order = 1)]
    public string SenderID { get; set; }

    [ForeignKey("SenderID")]
    public virtual ApplicationUser Sender { get; set; }

    [Key, Column(Order = 2)]
    public string ReceiverID { get; set; }

    [ForeignKey("ReceiverID")]
    public virtual ApplicationUser Receiver { get; set; }
}

public class GroupModRequest : FriendRequest
{
    [Key, Column(Order = 3)]
    public Guid GroupID { get; set; }

    public virtual Group Group { get; set; }

}

Note: I also have 'LocationModRequests' which inherits from FriendRequests as well and is almost the same as GroupModRequests, if it matters.

0

There are 0 best solutions below