【问题标题】:How do you construct an Entity class to fetch another Entity from repository?你如何构造一个实体类来从存储库中获取另一个实体?
【发布时间】:2025-12-16 18:25:03
【问题描述】:

这是一个 C# 问题,使用基于 Asp.NET Boilerplate 构建的 .NET 框架。 再次强调,被问到的问题是“如何......”,所以如果提供的答案是一个 url 链接或关于如何完成某事的描述性解释,我将非常感激。 (不要通过显示系鞋带的照片来回答如何系鞋带的问题,也不要通过显示某人钓鱼的录音来回答“如何钓鱼”......)

由于这个问题非常基本(我不需要再次改写/重复标题),我将举一个例子。

如果我有一个论坛服务,并且我创建一个类来加载一个Thread。在该线程类内部应该是某种集合、数组、列表,甚至是Post 的数据库集,在构造时被拉取。

[Table("Thread", Schema = "dbo")]
public class ThreadModel
{
    [Key]
    public long Id { get; set; }

    public string Title { get; set; }

    //Idea 1
    //Value should automatically be pulled and cached the moment class connects to database
    public Post[] Posts { get; set; }

    //Idea 2
    //Post has a constructor to return all post that matches a thread id. While new tag keeps the return value constantly refreshed.
    public Post[] Posts { get { return new Post(this.Id) } }

    //Idea 3
    //Not sure how collection is supposed to work. Does it automatically just pull or will i need to make a method to request?
    public virtual ICollection<Post> Posts { get; set; }

    //Example constructor
    //When connected to database key-value pairs that match database labels will automatically get stored in class
    protected ThreadModel()
    {
        //Idea 1-A
        //Should be a value of null or empty if database yields no results
        Posts = new Post(); 
    }

    public ThreadModel(int threadid) : this()
    {
        //Idea 1-A
        Id = threadid;
        //new Post => returns all posts in db
        //Posts default value is all post in db
        Posts = Posts.Select(post => post.threadid == this.id)

        //Idea 3-A
        Posts = Posts.Get(post => post.threadid == this.id)

        //Idea 4
        Posts = new Posts().GetThread(threadid);
    }
}

附加问题

如果所有entities 都是通过继承Entity 创建的,那么我在什么时候接触到EntityFramework 和DbContext

我喜欢this example here,这是一个用户在尝试将 ABP 连接到他们的数据库时提交的。但是他们的示例没有显示父/子资源。我找不到他们用来创建该指南的指南,以及它与using ABP to fetch EntityFramework's DbContext 示例的关系

this 是如何工作的?我找不到这方面的说明或解释? (我要进入谷歌以获得这些机制的答案?)

[Table("AbpItems")]
public class Item : Entity
{
    [ForeignKey("PostId")]
    public Post Post { get; set; }
    public int PostId { get; set; }
}

this 如何融入/与abp's EntityFramework 结合?

我应该在哪里创建我的数据库表/类?该项目遵循 Core.csproj、Application.csproj 和 EntityFramework.csproj 程序集布局。但似乎每个示例都在解决方案的不同阶段或位置创建类。

【问题讨论】:

    标签: c# entity-framework aspnetboilerplate asp.net-boilerplate


    【解决方案1】:

    使用GetAllIncluding。见https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2617

    这里有一个完整的解决方案;

    namespace EbicogluSoftware.Forum.Threads
    {
        [Table("Threads")]
        public class Thread : FullAuditedEntity
        {
            [Required]
            [StringLength(500)]
            public virtual string Title { get; set; }
    
            [Required]
            [StringLength(2000)]
            public virtual string Text { get; set; }
    
            public virtual List<Post> Posts { get; set; }
    
            public Thread()
            {
                Posts = new List<Post>();
            }
        }
    
        [Table("Posts")]
        public class Post : FullAuditedEntity
        {
            [Required]
            [StringLength(2000)]
            public virtual string Text { get; set; }
        }
    
        public class ThreadDto
        {
            public string Title { get; set; }
    
            public string Text { get; set; }
    
            public List<PostDto> Posts { get; set; }
    
            public ThreadDto()
            {
                Posts = new List<PostDto>();
            }
        }
    
        public class PostDto
        {
            public string Text { get; set; }
        }
    
        public class ThreadAppService : IApplicationService
        {
            private readonly IRepository<Thread> _threadRepository;
    
            public ThreadAppService(IRepository<Thread> threadRepository)
            {
                _threadRepository = threadRepository;
            }
    
            public async Task<List<TenantListDto>> GetThreads()
            {
                var threads = await _threadRepository.GetAllIncluding(x => x.Posts).ToListAsync();
                return threads.MapTo<List<TenantListDto>>();
            }
        }
    }
    

    我应该在哪里创建我的数据库表/类?

    您可以在YourProject.Core.proj中创建它们

    【讨论】:

    • 您是否可以添加一些额外的信息或资源供我学习?我希望能够在其他项目中自己复制它。但我不明白你提供的答案。为什么有一个看起来与Entity 完全一样的Dto?当我写其余的服务时,我从哪个类中获取?这是否意味着我可以忽略ThreadDomainService?如果我的代码的其他部分要从 Thread 或 Post 值中获取,例如 Title(int)Thread.Post.Count,我应该从 ThreadAppService 中获取吗?我真的很习惯直接集成,所以我不明白
    • Id 和 ForeignId 应该如何工作?我仍然没有收到回复以帮助澄清我的理解不足,并且您附加的链接只有 1 句回复。没有人试图分解并解释他们的反应。从字面上看,就像您在上面所做的一样。我问了一个问题,并特意解释了我为什么问,我在寻找什么,以及多种资源如何相互矛盾。您给了我一个复制粘贴的解决方案,但没有回答问题。
    • @user3681384 矛盾在哪里?
    • 矛盾在于 ABP 建立在 EntityFramework 之上,但 ABP 中没有任何 EF 的原理图或基础存在。即使有一个“映射”工具可以在不同的数据库模式之间进行转换,整个“学习 EF 就不会出现问题”的方法确实是一个问题。
    • 没有矛盾。这是 EF Include 的包装。如果您坚持,可以使用GetAll().Include
    最近更新 更多