Entity Framework 4.1 InverseProperty Attribute

前端 未结 2 1841
轻奢々
轻奢々 2020-11-29 01:39

Just wanted to know more about RelatedTo attribute and I found out it has been replaced by ForeignKey and InverseProperty attributes i

2条回答
  •  粉色の甜心
    2020-11-29 02:14

    ForeignKey attribute pairs FK property with navigation property. It can be placed either on FK property or navigation property. It is equivalent of HasForeignKey fluent mapping.

    public class MyEntity
    {
        public int Id { get; set; }
    
        [ForeignKey("Navigation")]
        public virtual int NavigationFK { get; set; }
    
        public virtual OtherEntity Navigation { get; set; }
    }
    

    InverseProperty is used for defining self referencing relation and pairing navigation properties on both ends. Check this question for sample.

提交回复
热议问题