EF 4.4 InverseProperty并非完全相反 [英] EF 4.4 InverseProperty does not quite inverse

查看:116
本文介绍了EF 4.4 InverseProperty并非完全相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这也许确实很明显,但是我不确定为什么我的InverseProperty注释不能以其他方式起作用。

This might really be something obvious but I'm not sure why my InverseProperty annotation does not work the other way.

我有这两个类(简化了):

I have this 2 classes (simplified):

public class Cluster
{
    [Key]
    public int ClusterId { get; set; }

    [Required]
    [MaxLength(80, ErrorMessage = "DimensionCluster Name cannot be more than 80 characters in length.")]
    [Display(Name = "DimensionCluster Name")]
    public string ClusterName { get; set; }

    [InverseProperty("DimensionCluster")]
    public virtual IEnumerable<Dimension> Dimensions { get; set; }

}

public class Dimension
{
    [Key]
    public int DimensionId { get; set; }

    [Required]
    [MaxLength(80, ErrorMessage = "Dimension Name cannot be more than 80 characters in length.")]
    [Display(Name = "Dimension Name")]
    public string DimensionName { get; set; }

    [Required]
    [Display(Name = "Short Definition")]
    public string ShortDefinition { get; set; }

    [Required]
    [Display(Name = "DimensionCluster Name")]
    public int ClusterId { get; set; }

    [ForeignKey("ClusterId")]
    public virtual Cluster DimensionCluster { get; set; }


}

上面的InverseProperty注释不起作用。我得到:

The InverseProperty annotation above does not work. I get:


属性 Dimensions中类型为
的InversePropertyAttribute PDguide.Models.Cluster无效。属性 DimensionCluster
不是相关类型
System.Collections.Generic.IEnumerable`1 [PDguide.Models.Dimension]上的有效导航属性。
确保该属性存在并且是有效的引用或集合
导航属性。

The InversePropertyAttribute on property 'Dimensions' on type 'PDguide.Models.Cluster' is not valid. The property 'DimensionCluster' is not a valid navigation property on the related type 'System.Collections.Generic.IEnumerable`1[PDguide.Models.Dimension]'. Ensure that the property exists and is a valid reference or collection navigation property.

尝试了很多MSDN文档和SO答案。我终于以另一种方式尝试了一下(下面的代码),并且成功了!

Tried a lot of MSDN documents and SO answers. And I finally tried it the other way (code below), and it worked!

public class Cluster
{
    [Key]
    public int ClusterId { get; set; }

    [Required]
    [MaxLength(80, ErrorMessage = "DimensionCluster Name cannot be more than 80 characters in length.")]
    [Display(Name = "DimensionCluster Name")]
    public string ClusterName { get; set; }

    public virtual IEnumerable<Dimension> Dimensions { get; set; }

}

public class Dimension
{
    [Key]
    public int DimensionId { get; set; }

    [Required]
    [MaxLength(80, ErrorMessage = "Dimension Name cannot be more than 80 characters in length.")]
    [Display(Name = "Dimension Name")]
    public string DimensionName { get; set; }

    [Required]
    [Display(Name = "Short Definition")]
    public string ShortDefinition { get; set; }

    [Required]
    [Display(Name = "DimensionCluster Name")]
    public int ClusterId { get; set; }

    [ForeignKey("ClusterId")]
    [InverseProperty("Dimensions")]
    public virtual Cluster DimensionCluster { get; set; }

}

我在某处阅读(找不到该参考文献现在,或者我可能不正确地推断出它),您可以在关系的任何一端指定InverserProperty批注。但是这里似乎不是这样吗?

I have read somewhere (I could not find that reference now, or I may have inferred it incorrectly) that you can specify the InverserProperty annotation on either end of the relationship. But that does not seem to be the case here?

我是正确的,我理解InverseProperty应该与这两个属性一起使用吗?

Am I right in my understanding that InverseProperty should work with either property?

推荐答案

您是对的。在Lerman和Miller编写的 Programming Entity Framework:代码优先
中,它在第72页上说

You are right. In Programming Entity Framework: Code First by Lerman and Miller it says on page 72


您可以将注释放置在关系的任一端(或者如果您需要
,则可以在两端)。

You can place the annotations on either end of the relationship (or both ends if you want).

当我查看当前的EF来源时,似乎只有 ICollection< T> 被识别为有效的逆属性。因此,我认为将 Dimensions 属性的类型更改为 ICollection< Dimension> 可以使您放置<$还有c $ c> InversePropertyAttribute 。

When I look in the current EF source, it seems that only collection properties of type ICollection<T> are recognized as valid inverse properties. So I think that changing the type of your Dimensions property into ICollection<Dimension> would allow you to put the InversePropertyAttribute there as well.

这篇关于EF 4.4 InverseProperty并非完全相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆