【发布时间】: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