C# 如何构造一个实体类来从存储库中获取另一个实体?
这是一个C#问题,使用基于Asp.NET样板的.NET框架。 再次强调的是,所问的问题是“如何…”,因此,如果提供的答案是url链接或关于应该如何做的描述性解释,我将非常感谢。(不要通过展示系好的鞋子的图片来回答有关如何系鞋带的问题,也不要通过展示某人钓鱼的录音来回答“如何钓鱼…” 因为这个问题非常基本(我不需要重新措辞/重复标题),所以我将给出一个示例 如果我有一个论坛服务,并且我创建了一个类来加载C# 如何构造一个实体类来从存储库中获取另一个实体?,c#,entity-framework,aspnetboilerplate,asp.net-boilerplate,C#,Entity Framework,Aspnetboilerplate,Asp.net Boilerplate,这是一个C#问题,使用基于Asp.NET样板的.NET框架。 再次强调的是,所问的问题是“如何…”,因此,如果提供的答案是url链接或关于应该如何做的描述性解释,我将非常感谢。(不要通过展示系好的鞋子的图片来回答有关如何系鞋带的问题,也不要通过展示某人钓鱼的录音来回答“如何钓鱼…” 因为这个问题非常基本(我不需要重新措辞/重复标题),所以我将给出一个示例 如果我有一个论坛服务,并且我创建了一个类来加载线程。在这个线程类中应该是某种集合、数组、列表,甚至是一个在构造上拉入的Post的dbset [
线程
。在这个线程类中应该是某种集合、数组、列表,甚至是一个在构造上拉入的Post
的dbset
[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);
}
}
如何集成到/与
我应该在哪里创建数据库表/类?该项目遵循核心.csproj、应用程序.csproj和实体框架.csproj组装布局。但似乎每个示例都在解决方案的不同阶段或位置创建类。使用
GetAllIncluding
。看
这里有一个完整的解决方案
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>>();
}
}
}
命名空间ebicglussoftware.Forum.Threads
{
[表(“螺纹”)]
公共类线程:FullAuditedEntity
{
[必需]
[长度(500)]
公共虚拟字符串标题{get;set;}
[必需]
[秘书长(2000年)]
公共虚拟字符串文本{get;set;}
公共虚拟列表发布{get;set;}
公共线程()
{
Posts=新列表();
}
}
[表(“员额”)]
公共类职位:FullAuditedEntity
{
[必需]
[秘书长(2000年)]
公共虚拟字符串文本{get;set;}
}
公共类ThreadDto
{
公共字符串标题{get;set;}
公共字符串文本{get;set;}
公共列表发布{get;set;}
公共线程DTO()
{
Posts=新列表();
}
}
公营邮递员
{
公共字符串文本{get;set;}
}
公共类ThreadAppService:IApplicationService
{
私有只读存储库;
公共ThreadAppService(IRepository threadRepository)
{
_threadRepository=threadRepository;
}
公共异步任务GetThreads()
{
var threads=wait_threadRepository.GetAllIncluding(x=>x.Posts).toListSync();
返回threads.MapTo();
}
}
}
我应该在哪里创建数据库表/类?
您可以在
YourProject.Core.proj
中创建它们,您是否可以添加一些额外的信息或资源供我学习?我希望自己能够在其他项目中复制这一点。但我不明白你提供的答案。为什么有一个与实体
一模一样的Dto
?当我编写服务的其余部分时,从哪个类获取?这是否意味着我可以忽略ThreadDomainService
?如果我的代码的其他部分要从线程或Post值获取,如Title
或(int)Thread.Post.Count
,我应该从ThreadAppService
获取吗?我真的习惯于直接集成,所以我不明白Id和异化应该如何工作?我仍然没有收到回复,以帮助澄清我的理解不足,你所附的链接只有一句话的回复。没有人试图细分并解释他们的反应。就像你刚才做的一样。我问了一个问题,然后不厌其烦地解释了我为什么要问,我在寻找什么,以及多种资源是如何相互矛盾的。你给了我一个复制粘贴解决方案,但没有回答问题。@ USS3681388:哪里是矛盾?矛盾是ABP是建立在EntityFramework之上的,但是EF的示意图或基础都没有存在于ABP中。即使有一个“映射”工具在不同的数据库模式之间进行转换,整个“学习EF,你就不会有问题”的方法也确实是一个问题,没有矛盾。这是EFInclude
的包装。您可以使用GetAll()。如果您坚持,请包括。
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>>();
}
}
}