One to One Relationship on Primary Key with Entity Framework Code First

后端 未结 1 1305
无人共我
无人共我 2021-01-21 10:23

I\'m currently getting the following error when trying to create an one to one relationship using Code First: System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in R

相关标签:
1条回答
  • 2021-01-21 11:04

    It is possible to place the ForeignKey attribute either on a navigation property and then specify the name of the property you want to have as the foreign key (that's what you did). Or you can place it on the foreign key property and then specify the name of the navigation property which represents the relationship. This would look like:

    public class C001_Holding_Test
    {
        [Key]
        [ForeignKey("C001_Holding")]
        public int C001_Id { get; set; }
    
        [MaxLength(100)]
        public string C001_TestInfo { get; set; }
    
        public virtual C001_Holding C001_Holding { get; set; }
    }
    

    For some reason this second option works while the first throws an error. (It feels like a bug to me because both options should represent the same relationship. Or there is actually a semantic difference which I don't see...)

    0 讨论(0)
提交回复
热议问题