问题
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; }
}
The InverseProperty annotation above does not work. I get:
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.
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; }
}
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?
Am I right in my understanding that InverseProperty should work with either property?
回答1:
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).
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.
来源:https://stackoverflow.com/questions/18625631/ef-4-4-inverseproperty-does-not-quite-inverse