【问题标题】:EF Code First not setting foreign key in one to one relationshipEF Code First 未在一对一关系中设置外键
【发布时间】:2017-06-20 14:59:35
【问题描述】:

所以我尝试创建一个简单的 Product-Preview 1 对 1 关系,如下所示:

public class Product : BaseEntity
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]        
    public Guid Id { get; set; }
    public string name { get; set; }
    public virtual EPS eps { get; set; }
    public virtual Preview preview { get; set; }

    [ForeignKey("userId")]
    public virtual User user { get; set; }
    public Guid userId { get; set; }

}

public class Preview : BaseEntity
{
    [Key,ForeignKey("Product")]        
    public Guid Id { get; set; }
    public string imagePath { get; set; }
    public double width { get; set; }
    public double height { get; set; }
    public virtual List<TextPreview> Texts { get; set; }
    public virtual List<ImagePlaceHolder> ImagePlaceHolders { get; set; }
    [ForeignKey("ProductId")]
    public virtual Product Product { get; set; }
    public virtual Guid ProductId { get; set; }
}

我希望在 Previews 表中有一个指向 Product 的外键 但在运行迁移后,我只是将它作为常规字段

我做错了什么?

【问题讨论】:

    标签: sql ef-code-first foreign-keys


    【解决方案1】:

    你几乎已经完成了,你只是错过了一块拼图......

    [ForeignKey("ProductId")]
    public virtual Product Product { get; set; }
    

    您还需要添加...

    public Guid ProductId { get; set; }
    

    到预览对象。

    还值得注意的是,ForeignKey 属性可以放在任一属性上,并且字符串必须引用该对中的另一个。

    正如目前所写,您正试图让 Id 属性为相关表的主键和外键指定值。

    所以你的最终代码可能看起来像...

    public class Product : BaseEntity
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]        
        public Guid Id { get; set; }
        [ForeignKey("User")]
        public Guid UserId { get; set; }
        public string name { get; set; }
    
        public virtual EPS eps { get; set; }
        public virtual Preview preview { get; set; }
        public virtual User user { get; set; }
    }
    

    public class Preview : BaseEntity
    {
        [Key]        
        public Guid Id { get; set; }
    
        [ForeignKey("Product")]
        public Guid ProductId { get; set; }
    
        public string imagePath { get; set; }
        public double width { get; set; }
        public double height { get; set; }
    
        public virtual List<TextPreview> Texts { get; set; }
        public virtual List<ImagePlaceHolder> ImagePlaceHolders { get; set; }
    
        public virtual Product Product { get; set; }
    }
    

    作为旁注,我还建议不要使用像 List&lt;T&gt; 这样的具体集合类型,而是使用像 IList&lt;T&gt;ICollection&lt;T&gt; 这样的东西,它可以促进更好的代码重用和可扩展性。

    【讨论】:

    • 感谢您的帮助,但如果我遵循您的建议,我最终会遇到此错误:Multiplicity is not valid in Role 'Preview_product_Source' in relationship 'Preview_product'。因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须是“*”。
    • 这个问题解决了,你现在有一个不同的问题,这里解释了......stackoverflow.com/questions/9292738/…
    • 基本上你有一个 1 对 1 的关系,看起来问题可能是你想要的是一个 0 对 1 的关系......使 guid ProductId 可以为空“Guid?”可以解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多