MVC与codefirst的一对一关系

时间:2017-06-11 12:50:57

标签: c# entity-framework ef-code-first

我试图与我的模特建立一对一的关系。但是我得到了以下错误:“关系'DetayAcıklama_UrunBilgi'中的角色'DetayAcıklama_UrunBilgi_Target'中的多重性无效。由于Dependent Role属性不是关键属性,因此Dependent Role的多重性的上限必须为'*' 。“

我的模型如下:

public class Urun
    {
        [Key]
        public int UrunID { get; set; }
        [Required]
        public string UrunKodu { get; set; }

        [Required]
        public decimal Fiyat { get; set; }

        [ForeignKey("DetayBilgi")]
        public int DetayAcıklamaID { get; set; }
        public DetayAcıklama DetayBilgi { get; set; }

    }


public class DetayAcıklama
    {
        [Key]
        public int DetayAcıklamaID { get; set; }

        [Display(Name = "Açıklama")]
        public string AcıklamaTxt { get; set; }

        public decimal Fiyat { get; set; }

        [Required, ForeignKey("UrunBilgi")]
        public int UrunID { get; set; }
        public Urun UrunBilgi { get; set; }
    }

我错过了什么?提前致谢

1 个答案:

答案 0 :(得分:0)

您当前的实现没有说明哪个表依赖于哪个表..为此指定主键列作为外键:

    [Key, ForeignKey("UrunBilgi")]
    public int DetayAcıklamaID { get; set; }