0

For example, I have the following classes

class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Team> Teams { get; set; }
    public ICollection<Address> Addresses { get; set; }
}
class Team
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<User> Users { get; set; }
}
class Address
{
    public int Id { get; set; }
    public string AddressType { get; set; }
    public string Address { get; set; }
    public int UserId { get; set; }
}

I have a way to determine at runtime if property is navigation property using ObjectContext metadata thanks to entity framework check if property is navigation property

But what I need additionally is to know if property is many-to-many (like Teams property in the example above) or one-to-many (like Addresses). Is there a way to do this?

Community
  • 1
  • 1
Mykyta
  • 13
  • 2
  • `User` have `ICollection
    Addresses` it is one to many . if `public ICollection Users` have `ICollection
    Addresses` many to many . In other words if you have list in your class and inside you have another list it is many to many relation
    – Eldho Jul 03 '15 at 11:41
  • Thank you for the reply. Yes, I was also thinking about such approach. But the problem here is that other side of many-to-many relation may not have any navigation property defined. In example above Team may not have ICollection Users but it still can be set as many-to-many relation – Mykyta Jul 03 '15 at 12:17

1 Answers1

1

In case someone will need same thing - here is some way of doing it:

var navigationProperties = objectContext.MetadataWorkspace
                        .GetItems<EntityType>(DataSpace.OSpace)
                        .Single(p => p.FullName == typeof(User).FullName)
                        .NavigationProperties;

var one = navigationProperties.Where(navigationProperty => navigationProperty.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One).ToList();
var many = navigationProperties.Where(navigationProperty => navigationProperty.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many).ToList();
Mykyta
  • 13
  • 2