I hit an issue when trying to delete records due to FK constraints. I therefore went back to the drawing board and am trying to specify how the relationship should work.
The error
The ForeignKeyAttribute on property 'DeferredData' on type 'MemberDataSet' is not valid. The foreign key name 'DeferredDataId' was not found on the dependent type 'DeferredData'.
is telling you exactly what is wrong.
DeferredData.Id is not DeferredData.DeferredDataId
This is your problem.
Just removing the attribute will solve your problem as Entity Framework figures out foreign keys based on the name of your entities. If you want to keep the attributes, use:
[ForeignKey("Id")]
instead of
[ForeignKey("DeferredDataId")]
So:
public class MemberDataSet
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int? DeferredDataId { get; set; }
[ForeignKey("Id")]
public virtual DeferredData DeferredData { get; set; }
}
or change the Id of DeferredData
to be DeferredDataId
and not Id
A few notes about EF:
Id
are automatically Keys, so no need for the Key
attributeEdit:
For a One-to-many relationship you need an ICollection
public virtual ICollection MemberDataSets { get; set; }
Does UserProfile
have a UserId
property?