It seems to me like the ForeignKey
attribute is not working for me, but I guess I'm using it wrong ;)
It's easier to explain with code:
public class BaseCard
{
public int Id {get ; set; }
public int BaseCardId { get; set; }
public List<Skill> Skills { get; set; }
}
public class Skill
{
public int Id { get; set; }
public int BaseCardId { get; set; }
[ForeignKey("BaseCardId")]
public BaseCard BaseCard { get; set; }
}
When I try to fill these objects with the seed method, I'm getting this error:
INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Skills_dbo.BaseCards_BaseCardId". The conflict occurred in database "Database", table "dbo.BaseCards", column 'Id'.
It seems to me like the ForeignKey
in Skill
tries to point at the Id
column of BaseCards
instead of the BaseCardId
column, and I can't figure out why..
If I try to remove the "normal" Id
property of BaseCard
, and set the BaseCardId
as the PK (with attribute [Key]
), I get the following error:
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
Does anyone know how I can get this code to work so the property BaseCardId
from the Skill
class will point to the BaseCardId
property of BaseCard
, instead of apparently the Id
property?
Thanks in advance!