Entity Framework 6 One-Way relationship using Code First -
i have following scenario:
color class
public int id { get; set; } public string name { get; set; } public string hex { get; set; }widget class
public int id { get; set; } public int headerbackgroundcolorid { get; set; } public color headerbackgroundcolor { get; set; }
using code-first, trying create one-way relationship between widget color classes headerbackgroundcolor / headerbackgroundcolorid fields.
normally in config class:
this.hasoptional(r => r.headerbackgroundcolor ) .withmany(m => m.widgets) .hasforeignkey(fk => fk.headerbackgroundcolorid); but not intrested in adding widgets collection color class. tried this:
this.hasoptional(r => r.headerbackgroundcolor ) .withmany() .hasforeignkey(fk => fk.headerbackgroundcolorid); but throws validation error.
what's way right?
you getting error because headerbackgroundcolorid non-nullable int, cannot optional.
all need achieve you're looking turn foreign key nullable int...
public int? headerbackgroundcolorid { get; set; } because named foreign key match navigation property (headbackgroundcolorid , headerbackgroundcolor) follows code first conventions, not need create explicit mappings. making above change make relationship optional.
Comments
Post a Comment