Entity Framework Code First Foreign Key issue

c# data-annotations ef-code-first entity-framework foreign-keys

Question

It appears to me that theForeignKey attribute is not functioning for me, but I believe I am use it incorrectly;)

Coding makes it simpler to explain:

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; }
}

I keep getting the following problem while using the seed technique to fill these objects:

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 appears to me that theForeignKey in Skill attempts to identify theId a row ofBaseCards as opposed to theBaseCardId column, and I'm baffled as to why.

Trying to get rid of the "normal"Id possession ofBaseCard and then set theBaseCardId PK (with attribute) as[Key] ), I encounter the subsequent 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.

Can somebody tell me how to make this code function so that the propertyBaseCardId using theSkill Class will highlight theBaseCardId possession ofBaseCard instead of what appears to be theId property?

Thanks in advance!

1
5
12/27/2014 2:35:23 PM

Accepted Answer

ZZZ_tmp
9
5/23/2017 12:17:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow