c# - 使用 EF Core 继承时如何避免重复属性投影?

标签 c# entity-framework linq entity-framework-core

在使用 inheritance in EF Core 时,我正在努力避免重复投影逻辑.
这是我的场景:我有三种类型:

  • Lesson (这是一个抽象类)(属性: IdTitle 等)
  • ArticleLesson (继承自 Lesson )(属性: ContentTotalWoddsCount 等)
  • VideoLesson (继承自 Lesson )(属性: VideoUrlDuration 等)

  • EF Core 几乎可以正确处理所有内容,我使用的是默认的 Table-Per-Hierarchy (TPH) 方法。
    当我想从数据库中检索类(class)并且我需要一些 ArticleLesson 之间的共享列时,就会出现问题。和 VideoLesson (即 Lesson 的一些属性),以及一些特定于 ArticleLesson 的属性或 VideoLesson .这是我想出的表达方式:
    var r1 = dbContext.Lessons.Select<Lesson, LessonDto>(l =>
        l is ArticleLesson
        ? new ArticleLessonDto
        {
            Id = l.Id, // This is repeated below
            Title = l.Title, // This is repeated below
            // ...other properties that I would have to repeat below
            Content = (l as ArticleLesson).Content,
        }
        : l is VideoLesson
        ? new VideoLessonDto
        {
            Id = l.Id, // This is repeated above
            Title = l.Title, // This is repeated above
            // ...other properties that I would have to repeat above
            VideoUrl = (l as VideoLesson).VideoUrl,
        }
        : null
    )
    .ToList();
    
    如您所见,我重复了共享属性部分两次。在这个例子中只有 2 个属性重复,IdTitle ,但在现实世界中,您可以拥有数十个;并且不得不像这样重复所有这些将是一个小时。
    有什么办法可以让这个投影表达更简洁,避免重复?

    最佳答案

    您可以在 LessonDto 中添加一个构造函数, ArticleLessonDtoVideoLessonDto接受不同的共享属性。

        public class LessonDto
        {
            public LessonDto(int id, ... other)
            {
                Id = id;
                // ...
            }
    
            public int Id { get; set; }
        }
    
        public class ArticleLessonDto : LessonDto
        {
            public ArticleLessonDto(LessonDto dto) : base(dto.Id)
            {
    
            }
    
            public string Content { get; set; }
        }
    
        var r1 = dbContext.Lessons
            .Select(l => new
            {
                dto = new LessonDto(l.Id, ... other),
                full = l
            })
            .Select(row => row.full is ArticleLesson
            ? new ArticleLessonDto(row.dto)
            {
                Content = (row.full as ArticleLesson).Content,
            }
            : row.full is VideoLesson
            ? new VideoLessonDto(row.dto)
            {
                VideoUrl = (row.full as VideoLesson).VideoUrl,
            }
            : (LessonDto)null
        )
        .ToList();
    

    关于c# - 使用 EF Core 继承时如何避免重复属性投影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67322631/

    相关文章:

    c# - 种子方法如何使用foreach循环?

    entity-framework - 在添加包含第一个对象的对象时,如何防止 EF 插入数据库中已存在的对象?

    c# - LINQ to Entities 跳过并采取

    c# - 在 Linq 谓词中,编译器会优化对 Enumerable.Min() 的 "scalar"调用还是会为每个项目调用它?

    entity-framework - 使用 Entity Framework (核心)在 Linq 中进行左连接

    c# - 插入 SQL 数据库,在带有复选框列表的 foreach 中

    c# - WCF 中的超时问题

    c# - 恢复 ListView 状态 MVVM

    c# - "Encoded"SharePoint 客户端对象模型中的登录名

    .net - Entity Framework 类可以有非 Entity Framework 成员吗?