简体   繁体   English

是否有使用实体框架将同一接口的两个实体合并的解决方法?

[英]Is there a work around for unioning two entities of the same interface using entity framework?

I have a search model class that searches different entity sets with the entity itself implementing a IAssignable interface. 我有一个搜索模型类,可使用实现IAssignable接口的实体本身搜索不同的实体集。 The code looks like this. 代码看起来像这样。

public void Search()
    {
        List<T> lessons = new List<T>();
        List<T> courses = new List<T>();

        if (ShowLessons)
            lessons = db.Set<Lesson>()
                .Where(IAssignableExtensions.SearchPredicate(q))
                .Select(LessonMapping).ToList();
        if (ShowCourses)
            courses = db.Set<Course>()
                .Where(IAssignableExtensions.SearchPredicate(q))
                .Select(CourseMapping).ToList();
        Results = lessons.Union(courses).ToList<T>();
    }

The static extension is irrelevant, it just searched based on the query. 静态扩展名是无关紧要的,它只是基于查询进行搜索。 I would prefer to bust this into it's own rather than static extension but eh. 我宁愿将其破坏成它自己的,而不是静态扩展,但是。 Now this works as expected. 现在,这可以按预期工作。 I am pulling to memory two datasets, lessons and courses, I am unioning them into a IEnumerable of a generic type based on teh Course Mapping or Lesson Mapping Expressions. 我要记忆两个数据集,课程和课程,并根据课程映射或课程映射表达式将它们合并为一个通用类型的IEnumerable。

public Expression<Func<IAssignable, T>> LessonMapping { get; set; }
        public Expression<Func<IAssignable, T>> CourseMapping { get; set; }

The problem is when I want to do any type of paging. 问题是当我想执行任何类型的分页时。 As you can see the lessons and courses are searched, brought into memory and then unioned and returned. 如您所见,课程和课程被搜索,存储到内存中,然后合并并返回。 If I do any paging using an IPagedList for example, it is bringing back ALL lessons and courses then it is only using a subset of the total data in the list for the pages. 例如,如果我使用IPagedList进行任何分页,它将带回所有课程和课程,那么它仅使用页面列表中全部数据的一部分。

If Entity Framework supported interfaces I would just do a cast on the interface and union right at the db call. 如果实体框架支持接口,那么我只需要在接口上进行强制转换,然后在db调用处进行并集。 I haven't changed this code yet but I feel I might have to create a custom stored procedure or use the Query call on the datacontext, but if I use a stored procedure I have to make sure to update it on any changes to the domain, and if I use the Query I have to re-jig the selects, interfaces and still have to worry about inline sql... 我尚未更改此代码,但是我觉得可能必须创建一个自定义存储过程或对datacontext使用Query调用,但是如果使用存储过程,则必须确保对域的任何更改都对其进行更新,如果我使用查询,则必须重新选择,接口,而仍然需要担心内联sql ...

Anyone have any ideas? 有人有想法么?

UPDATE The solution that I ended up using after thinking about Erik's solution was to just use a projected object that implemented IAssignable. 更新在考虑了Erik的解决方案后,我最终使用的解决方案是仅使用实现IAssignable的投影对象。

public class SomeProjection : IAssignable
{
public int ID { get; set; }
public string Name { get; set; }
public string Description {get;set;}
public string Privacy {get;set;}
    }

And then used it within the union call queryable 然后在可查询的联合调用中使用它

    Results = db.Set<Lesson>().Select(p => new SomeProjection() { Privacy = p.Privacy, ID = p.ID, Name = p.Name, Description = p.Description })
        .Union(db.Set<Course>().Select(p => new SomeProjection() { Privacy = p.Privacy, ID = p.ID, Name = p.Name, Description = p.Description }))
        .Where(IAssignableExtensions.SearchPredicate(q))
            .Select(Mapping).ToList<T>();

If Entity Framework supported interfaces I would just do a cast on the interface and union right at the db call. 如果实体框架支持接口,那么我只需要在接口上进行强制转换,然后在db调用处进行并集。

It has nothing to do with what Entity Framework supports. 它与实体框架支持什么无关。 If you create an interface, it is independent of the SQL technology in the back end and you want EF to somehow magically select properties based on an interface with no mappings or configuration? 如果创建接口,则它独立于后端的SQL技术,并且您希望EF以某种方式基于没有映射或配置的接口神奇地选择属性吗? Not going to happen. 不会发生。

Instead you could simply use inheritance if there are some properties that are the same between objects , then you don't even need to union them, unless you are where-ing on properties that don't exist between both. 相反,你可以简单地使用继承,如果有一些特性是对象之间的相同 ,那么你甚至不需要工会它们,除非你是在不两间存在的所有属性,其中-ING。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM