Hvis du ikke ønsker at bruge flydende syntaks, er der tre andre måder at implementere referencen ved hjælp af dataannoteringer (Personligt foretrækker jeg dataannoteringer, da de virker lettere at læse og er skrevet lige over den egenskab, de påvirker):
1.1) Brug ForeignKey (med en tilhørende egenskab) - version 1
[Table("WIDGETENTITIES")]
public class WidgetEntity {
[Column("WIDGETENTITY_ID")]
public int Id { get; set; }
[Column("WIDGETSEQUENCE_ID")]
public int WidgetSequenceId { get; set; }
[ForeignKey("WidgetSequenceId")] //Has to be a property name, not table column name
public WidgetSequence Sequence { get; set; }
// and other properties that map correctly
}
[Table("WIDGETSEQUENCES")]
public class WidgetSequence {
[Column("WIDGETSEQUENCE_ID")]
public int Id { get; set; }
[Column("NUMBER")]
public int Number { get; set; }
}
1.2) Brug ForeignKey (med en tilhørende egenskab) - version 2
[Table("WIDGETENTITIES")]
public class WidgetEntity {
[Column("WIDGETENTITY_ID")]
public int Id { get; set; }
[ForeignKey("Sequence")] //Has to be a property name, not table column name
[Column("WIDGETSEQUENCE_ID")]
public int WidgetSequenceId { get; set; }
public WidgetSequence Sequence { get; set; }
// and other properties that map correctly
}
[Table("WIDGETSEQUENCES")]
public class WidgetSequence {
[Column("WIDGETSEQUENCE_ID")]
public int Id { get; set; }
[Column("NUMBER")]
public int Number { get; set; }
}
2)Du kan også bruge InversePropertyAttribute.
[Table("WIDGETENTITIES")]
public class WidgetEntity {
[Column("WIDGETENTITY_ID")]
public int Id { get; set; }
[InverseProperty("WidgetEntities")]
public WidgetSequence Sequence { get; set; }
// and other properties that map correctly
}
[Table("WIDGETSEQUENCES")]
public class WidgetSequence {
[Column("WIDGETSEQUENCE_ID")]
public int Id { get; set; }
[Column("NUMBER")]
public int Number { get; set; }
public virtual List<WidgetEntity> WidgetEntities { get; set; }
}