本文介绍了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:

尝试了很多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 Firstby Lerman and Miller it says on page 72

当我查看当前的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并非完全相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 18:22