Seeding a foreign key using code first in c# -
i having issue on seeding database , giving foreign key value.
this model userpains:
public partial class userpain { public int id { get; set; } public int scoredby { get; set; } public datetime lastupdated { get; set; } public virtual defect defects { get; set; } } this defects model:
public partial class defect { public int id { get; set; } public string title { get; set; } public string description { get; set; } } so when seed with:
new userpain { scoredby = 1, lastupdated = datetime.now } the value in userpain table labelled defects_id null. how assign value of defects_id id existing defect in defects table?
you can set defect property defect instance created or fetched database. alternatively can add id field userpain object , set instead:
public partial class userpain { // ... [column("defects_id")] public int defectid { get; set; } [foreignkey("defectid")] public virtual defect defect { get; set; } } now can set defectid without instance. singularized property name in process!
Comments
Post a Comment