I have two classes:
public class Address
{
public Guid Id { get; set; }
public virtual ICollection<ToAddressMessageLink> MessagesTo { get; set; }
public virtual ICollection<CopyAddressMessageLink> MessagesCopyTo { get; set; }
}
public class Message
{
public Guid Id { get; set; }
public virtual ICollection<ToAddressMessageLink> To { get; set; }
public virtual ICollection<CopyAddressMessageLink> CopyTo { get; set; }
}
And I need to save connection between them in a single table. If I simply put many-to-many relation between, EF wouldn't realize what type of connection is actually set(table rows will be identical for them). So I have to create a link class with a discriminator like following:
public class AddressMessageLink
{
public int LinkType { get; set; }
public Guid AddressId { get; set; }
public Guid MessageId { get; set; }
}
But it also doesn't work because I can't set link type/ So I have to use TPH here:
public abstract class AddressMessageLink
{
public int LinkType { get; set; }
public Guid AddressId { get; set; }
public Guid MessageId { get; set; }
}
public class CopyAddressMessageLink : AddressMessageLink
{
public virtual AddressDto Address { get; set; }
public virtual MessageDto Message { get; set; }
}
public class ToAddressMessageLink : AddressMessageLink
{
public virtual AddressDto Address { get; set; }
public virtual MessageDto Message { get; set; }
}
With a composite key:
HasKey(x=>new {x.AddressId, x.MessageId, x.LinkType});
But it doesn't work either, because EF:
"The foreign key component AddressId is not a declared property on type ToAddressMessageLink".
If I put AddressId
and MessageId
into derived class I can't set key because there is no component of it in base class.
What can I do it this situation?