This is similar to questions here and here, but those are old and have no good answers.
Let\'s say I have the following classes:
class HairCutStyle {
There are three things to ensure:
DbSet<HairCutStyle>
in your DbContext
-derived classHairCutStyle
in your OnModelCreating
overrideHairCutStyle
property using the NotMapped
attribute.Add a property in CustomerHairCutPreference
for HairCutSytleID
and then use the [NotMapped]
attribute on the HairCutStyle
property. Note, however, that you will then be responsible for ensuring that the HairCutStyle
and HairCutStyleID
stay in sync.
class CustomerHairCutPreference {
public int ID { get; set; }
public Customer Customer { get; set; }
public int HairCutStyleID {get; set; }
[NotMapped]
public HairCutStyle HairCutStyle { get; set; }
}
Alternatively, you can use the FluentAPI to exclude HairCutStyle
completely from ever being mapped by Entity Framework, which may be useful if you have multiple classes that link to it.
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Ignore<HairCutStyle>();
}