[英]Extend Repository Class C# specific to Entity
I currently have a entity framework repository class that has been based upon the below code I found online. 我目前有一个实体框架存储库类,该类基于我在网上找到的以下代码。
https://gist.github.com/ashrafeme/060f7773e25903ced986804ee7276f5f https://gist.github.com/ashrafeme/060f7773e25903ced986804ee7276f5f
This works fine, however, I'd like to create additional methods that perform specific functions for specific entities. 这很好用,但是,我想创建其他方法来为特定实体执行特定功能。 For example, if I had a blog post that needed to filter by Post Type, I could write in my controller
例如,如果我有一个博客帖子需要按帖子类型过滤,则可以在控制器中编写
SqlRepository<Blog, MyContext> blogPostRepo = new SqlRepository<Blog, MyContext>(dbContext);
LinkedList<Blog> blogPosts = (LinkedList<Blog>)blogPostRepo.Set<Blog>().Where(p => p.PostType.Id.Equals(2));
However Ideally I would like to just extend the Base SqlRepository Class to create a BlogPostRepository to try and keep the code clean. 但是理想情况下,我只想扩展Base SqlRepository类以创建BlogPostRepository来尝试保持代码干净。 I'm getting stuck at the minute with the class signatures in my BlogPostRepository.
我一时被BlogPostRepository中的类签名卡住了。 How would I write a class that extends the below base class/
我将如何编写一个扩展以下基类的类/
public class SqlRepository<TEntity, TContext> : IRepository<TEntity>, IDisposable
where TEntity : class
where TContext : DbContext
{
enter code here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.