EF5 How to get list of navigation properties for a domain object

后端 未结 4 711
一整个雨季
一整个雨季 2020-12-06 14:09

I\'m working on an EF 5 Code-First solution and I am trying to update an existing entity with a modified one using the Repository pattern:

    public void Up         


        
4条回答
  •  情深已故
    2020-12-06 14:45

    Ah, the glories of LINQ (and using):

    public List GetNavigationProperties(T entity)
    {
        var t = entity.GetType();
        var elementType = ((IObjectContextAdapter)context).ObjectContext.CreateObjectSet().EntitySet.ElementType;
        return elementType.NavigationProperties.Select(np => entityType.GetProperty(np.Name)).ToList();
    }
    

    This can also be implemented via a static method, with the following signature:

    public static List GetNavigationProperties(DbContext context)
    {
        var t = typeof(T);
        ...
    

提交回复
热议问题