[英]Entity Framework 6 way to check at runtime if navigation property is many-to-many or one-to-many
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 我有一种方法可以在运行时使用ObjectContext元数据确定属性是否为导航属性,这要归功于实体框架检查属性是否为导航属性
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). 但是我还需要知道属性是多对多(例如上面的示例中的Teams属性)还是一对多(例如地址)。 Is there a way to do this?
有没有办法做到这一点?
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.