Abp Vnext手动搭建简单项目系列4

数据库访问——数据插入数据库

摸索了好几天有点忘记这个功能做了哪些代码改动,附上源码git仓库:https://github.com/1259724620/hqh.project20200430.git

一、定义仓储接口和仓储实现

在项目01.hqh.project.EntityFrameCore定义仓储接口

 public interface IUserRepository : IRepository<User>
    {

    }

在项目02.hqh.project.EntityFrameCore.DbMigration定义仓储实现

1、先定义仓储实现基类

namespace hqh.project.EntityFrameCore.DbMigration.Repositories
{
    public abstract class RepositoryBase<Entity,PrimaryKey>:EfCoreRepository<HqhProjectDbContext, Entity, PrimaryKey> 
        where Entity: class, IEntity<PrimaryKey>
    {
        protected RepositoryBase(IDbContextProvider<HqhProjectDbContext> dbContextProvider)
          : base(dbContextProvider)
        {

        }
    }

    public abstract class HqhProjectRepositoryBase<TEntity>: RepositoryBase<TEntity,int>
         where TEntity : class, IEntity<int>
    {
        protected HqhProjectRepositoryBase(IDbContextProvider<HqhProjectDbContext> dbContextProvider)
           : base(dbContextProvider)
        {

        }
    }
}

2、定义User仓储

using hqh.project.EntityFrameCore.DbMigration.EntityFrameCore;
using hqh.project.EntityFrameCore.IRepositories;
using Volo.Abp.EntityFrameworkCore;

namespace hqh.project.EntityFrameCore.DbMigration.Repositories
{
    public class UserRepository : HqhProjectRepositoryBase<User>, IUserRepository
    {
        public UserRepository(IDbContextProvider<HqhProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
    }
}

3、该项目02.hqh.project.EntityFrameCore.DbMigration模块类添加如下依赖

 三、定义服务接口和服务实现

1、服务项目模块类添加如下依赖

 4、添加公共项目,参考git源码库

 5、添加接口,插入数据成功,参考git源码库

猜你喜欢

转载自www.cnblogs.com/come-on-come-on/p/12803005.html
ABP