书接上文:Abp 从空白的WebApplication中添加EntityFrameworkCore生成数据库
开发环境:.NET6、Volo.Abp
数据库:Sqlite
说明:纯属个人强行入门。我个人觉得按照官网的操作不舒服,所以自己研究着来,请读者根据自己的需要进行参考。我能保证的是按照文章操作能够得到和我一样的结果。
1、在应用层中添加项目DemoApplication 项目目录如下图所示:
1.1、项目中引入包情况如下:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.AspNetCore" Version="6.0.3" />
<PackageReference Include="Volo.Abp.Autofac" Version="6.0.3" />
<PackageReference Include="Volo.Abp.AutoMapper" Version="6.0.3" />
<PackageReference Include="Volo.Abp.Ddd.Application" Version="6.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DemoEntityFrameworkCore\DemoEntityFrameworkCore.csproj" />
</ItemGroup>
</Project>
1.2、创建DemoApplicationCoreModule类,代码如下:
using DemoApplication.Book;
using DemoEntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application;
using Volo.Abp.Autofac;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity;
namespace DemoApplication
{
[DependsOn(typeof(AbpAutoMapperModule), typeof(AbpAutofacModule))]
public class DemoApplicationCoreModule:AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpAutoMapperOptions>(options =>
{
options.AddMaps<DemoApplicationCoreModule>();
});
}
}
}
1.3、创建类BookInfoAppService,代码如下:
using DemoDomain.Book;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Autofac;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity;
namespace DemoApplication.Book
{
[DependsOn(typeof(AbpAutofacModule))]
public class BookInfoAppService:ApplicationService,ITransientDependency
{
private readonly IRepository<BookInfo, Guid> _bookInfoRepository;
public BookInfoAppService(IRepository<BookInfo, Guid> bookInfoRepository)
{
_bookInfoRepository = bookInfoRepository;
}
public List<BookInfo> Get()
{
long count= _bookInfoRepository.GetCountAsync().Result;
List<BookInfo> listBookInfos= _bookInfoRepository.GetListAsync().Result;
return listBookInfos;
}
}
}
1.4、目录中BookInfoDTO类目前没有使用到,也把代码粘上来吧:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication.Book.Dto
{
public class BookInfoDTO
{
public string Name { get; set; }
public string Description { get; set; }
}
}
1.5、 目录中ApplicationAutoMapperProfile类目前没有使用到,也把代码粘上来吧:
using AutoMapper;
using DemoApplication.Book.Dto;
using DemoDomain.Book;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Autofac;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity;
namespace DemoApplication
{
[DependsOn(typeof(AbpAutofacModule))]
public class ApplicationAutoMapperProfile:Profile
{
public ApplicationAutoMapperProfile()
{
//创建一个实体的映射
CreateMap<BookInfo, BookInfoDTO>();
}
}
}
2、对于用户接口层要做的修改。
2.1、修改DemoAbpModule类中代码,具体代码如下:
using Volo.Abp.Modularity;
using Volo.Abp.Autofac;
using Volo.Abp.AspNetCore;
using Volo.Abp;
using DemoApplication;
using DemoApplication.Book;
using Volo.Abp.AutoMapper;
using DemoEntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Auditing;
using Microsoft.Extensions.DependencyInjection.Extensions;
using DemoDomain;
namespace DemoAspNetCoreApplict
{
[DependsOn(
typeof(AbpAspNetCoreModule),
typeof(AbpAutofacModule),
typeof(DemoDomainAbpModule),
typeof(DemoEntityFrameworkCroeAbpModule),
typeof(DemoApplicationCoreModule))]
public class DemoAbpModule:AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
base.PreConfigureServices(context);
var hostingEnviroment = context.Services.GetHostingEnvironment();
var configuration = context.Services.GetConfiguration();
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app=context.GetApplicationBuilder();
var env=context.GetEnvironment();
if(env.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseConfiguredEndpoints();
//app.UseAuthorization();
base.OnApplicationInitialization(context);
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
base.ConfigureServices(context);
context.Services.AddControllers();
context.Services.AddDbContext<DemoDbContext>(options =>
{
options.UseSqlite("Data Source=E:\\ABP\\demo.db;");
});
Configure<AbpAutoMapperOptions>(options =>
{
options.AddMaps<DemoApplicationCoreModule>();
});
context.Services.Configure<AbpAuditingOptions>(options =>
{
options.IsEnabled = true;
});
}
}
}
2.2、注意需要添加依赖对象,不然你会收到意想不到的意外哦。具体如下:
typeof(DemoDomainAbpModule),
typeof(DemoEntityFrameworkCroeAbpModule),
typeof(DemoApplicationCoreModule)
2.3、添加一个控制器GetBookInfoController,具体代码如下:
using DemoApplication.Book;
using DemoApplication.Book.Dto;
using DemoDomain.Book;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Volo.Abp.AspNetCore.Mvc;
namespace DemoAspNetCoreApplict
{
[ApiController]
[Route("api/[controller]/[action]")]
public class GetBookInfoController: AbpController
{
private readonly ILogger<GetBookInfoController> _logger;
private readonly BookInfoAppService _bookInfoAppService;
public GetBookInfoController(ILogger<GetBookInfoController> logger, BookInfoAppService bookInfoAppService)
{
_logger = logger;
_bookInfoAppService = bookInfoAppService;
}
[HttpGet]
public IEnumerable<BookInfo> Get()
{
List<BookInfo> bookInfo = _bookInfoAppService.Get().ToList();
return bookInfo;
}
}
}
3、具体的可执行代码已上传,想赚点小钱钱喽有需要的请去下载吧。
https://download.csdn.net/download/xingchengaiwei/888151984
4、推广一个云服务器,买服务器的私聊我送源代码呦。
开发云 - 一站式云服务平台