I am currently making an EF migration script, on a schema model that looks like this
public class Schema : RootId
{
public int Version { get; set; }
[ForeignKey("entity_id")]
public virtual ICollection<Entity> Entities { get; set; } = null!;
[ForeignKey("entity_id")]
public virtual ICollection<Entity> Relations { get; set; } = null!;
}
Here both relations uses the same object type, and when I do an migration I get the error
There are multiple navigations in entity type 'Schema' which are pointing to same set of properties using a [ForeignKey] attribute: 'entity_id'
which is true as the foreign key for both of them is entity_id.
but this has worked before, but back then the type was ICollection<Relation>, which was a complete copy of the Entity, which does not make sense, since this introduces code duplication.
How do I convert Relations to an ICollection<Entity> and still be able to use the foreing_key entity_id - what i don't get is how changing the type make it confused?
Update:
Ok, i tried doing it like this then
public class Schema : RootId
{
public int Version { get; set; }
[ForeignKey("entity_id")]
public virtual ICollection<Entity> Entities { get; set; } = null!;
[ForeignKey("entity_id")]
public virtual ICollection<Relation> Relations { get; set; } = null!;
}
public class Entity : BaseEntity
{
}
public class Relation : BaseEntity
{
}
public class BaseEntity : RootId
{
}
but this then causes and foreign key violation, everytime I insert or update the table?...
"Key (attribute_entity_id)=(4) is not present in table \"relation\"."