InverseProperty doesn't work with protected or internal properties RRS feed

  • Question

  • I have the following Entity Models

    public class User
    public virtual long Id{get; set;}
    public virtual ICollection<Tag> Tags { get; protected set; }

    public class Tag
    public virtual long Id{get; set;}
    internal protected virtual ICollection<User> Users { get;  set; }

    This is pure simple many-to-many relation User & Tag I'm using Data Migrations. When I execute the command Add-Migration or Update-Database I get the following error "The InversePropertyAttribute on property 'Tags' on type 'Kigg.DomainObjects.Entities.User' is not valid. The property 'Users' is not a valid navigation property on the related type 'Kigg.DomainObjects.Entities.Tag'. Ensure that the property exists and is a valid reference or collection navigation property."

    When I changed the access modifier of Users property in Tag to public it worked fine and the generation is what I want.

    From my design point of view I want to hide the Tag.Users property and make it protected or internal to keep it for internal use as I don't want to expose it to public API.

    Note: I'm not discussing the my design here. I'm asking if it's possible to do that while Tag.Users is protected or internal?

    Muhammad Mosa

    Monday, March 26, 2012 12:18 PM


  • Hi Muhammad Soliman,

    Welcome to MSDN Forum.

    I have tested the code, and based on my test, the IversePropertyAttribute can't be added to a internal property or a protected property.

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 29, 2012 2:36 AM