书接上文:Abp 从空白WebApplication开始
开发环境:.NET6、Volo.Abp
数据库:Sqlite
说明:纯属个人强行入门。我个人觉得按照官网的操作不舒服,所以自己研究着来,请读者根据自己的需要进行参考。我能保证的是按照文章操作能够得到和我一样的结果。
1、项目搭建,将项目分为以下几个层
1.1、用户接口层:目前放了空白的WebApplication,理论上不应该这样,先这么放着吧。项目名称DemoAspNetCoreApplict,少了几个字母,哈哈。
1.2、应用层:目前是空的
1.3、领域层:创建库项目,项目名称DemoDomain。
1.4、基础设施层:创建库项目,项目名称DemoEntityFrameworkCore。
2、创建领域层项目,项目目录如下图:
2.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.Ddd.Domain" Version="6.0.3" /></ItemGroup></Project>
2.2、创建文件夹Book,在文件夹Book中添加类BookInfo.cs,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities.Auditing;namespace DemoDomain.Book
{public class BookInfo:FullAuditedAggregateRoot<Guid>{public string Name { get; set; }public string Description { get; set; }}
}
2.3、添加类DemoDomainAbpModule.cs,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain;
using Volo.Abp.Modularity;namespace DemoDomain
{[DependsOn(typeof(AbpDddDomainModule))]public class DemoDomainAbpModule:AbpModule{}
}
3、创建基础设施层项目,项目目录请看第一张图:
3.1、项目中引入包情况如下:
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net6.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable></PropertyGroup><ItemGroup><PackageReference Include="Volo.Abp.AspNetCore.Mvc" Version="6.0.3" /><PackageReference Include="Volo.Abp.EntityFrameworkCore" Version="6.0.3" /><PackageReference Include="Volo.Abp.EntityFrameworkCore.Sqlite" Version="6.0.3" /></ItemGroup><ItemGroup><ProjectReference Include="..\DemoDomain\DemoDomain.csproj" /></ItemGroup></Project>
3.2、添加DemoEntityFrameworkCroeAbpModule.cs类,代码如下:
using DemoDomain;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Sqlite;
using Volo.Abp.Modularity;namespace DemoEntityFrameworkCore
{[DependsOn(typeof(DemoDomainAbpModule),typeof(AbpEntityFrameworkCoreModule), typeof(AbpEntityFrameworkCoreSqliteModule))]public class DemoEntityFrameworkCroeAbpModule:AbpModule{public override void ConfigureServices(ServiceConfigurationContext context){base.ConfigureServices(context);Configure<AbpDbContextOptions>(opt =>{opt.UseSqlite();});var services = context.Services;services.AddAbpDbContext<DemoDbContext>(options =>{ options.AddDefaultRepositories(true);});}}
}
3.3、添加DemoDbContext.cs类,注意这里的BookInfom目前还未添加代码如下:
using DemoDomain.Book;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;namespace DemoEntityFrameworkCore
{[ConnectionStringName("conn_mssql")]public class DemoDbContext : AbpDbContext<DemoDbContext>{public DbSet<BookInfo> BookInfos { get; set; }public DemoDbContext(DbContextOptions<DemoDbContext> options) : base(options){}protected override void OnModelCreating(ModelBuilder modelBuilder){base.OnModelCreating(modelBuilder);}}
}
3.4、以下内容是生成数据库时产生的不用理会:
4、 创建用户接口层项目,项目目录请看第一张图:
4.1、项目中引入包情况如下:
<Project Sdk="Microsoft.NET.Sdk.Web"><PropertyGroup><TargetFramework>net6.0</TargetFramework><Nullable>enable</Nullable><ImplicitUsings>enable</ImplicitUsings></PropertyGroup><ItemGroup><PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.26"><PrivateAssets>all</PrivateAssets><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets></PackageReference><PackageReference Include="Volo.Abp.AspNetCore.Mvc" Version="6.0.3" /><PackageReference Include="Volo.Abp.Autofac" Version="6.0.3" /></ItemGroup><ItemGroup><ProjectReference Include="..\DemoEntityFrameworkCore\DemoEntityFrameworkCore.csproj" /></ItemGroup></Project>
4.2、添加类DemoAbpModule.cs,代码如下:
using Volo.Abp.Modularity;
using Volo.Abp.Autofac;
using Volo.Abp.AspNetCore;
using Volo.Abp;namespace DemoAspNetCoreApplict
{[DependsOn(typeof(AbpAspNetCoreModule),typeof(AbpAutofacModule))]public class DemoAbpModule:AbpModule{public override void OnApplicationInitialization(ApplicationInitializationContext context){//base.OnApplicationInitialization(context);var app=context.GetApplicationBuilder();var env=context.GetEnvironment();if(env.IsDevelopment()){app.UseExceptionHandler("/Error");app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles(); app.UseRouting();app.UseConfiguredEndpoints();}}
}
4.3、修改Program.cs类,代码如下:
using DemoAspNetCoreApplict;
using DemoEntityFrameworkCore;
using Microsoft.EntityFrameworkCore;var builder = WebApplication.CreateBuilder(args);
builder.Host.UseAutofac(); //Add this line
builder.Services.ReplaceConfiguration(builder.Configuration);
builder.Services.AddApplication<DemoAbpModule>();
builder.Services.AddDbContext<DemoDbContext>(options =>
{options.UseSqlite("Data Source=E:\\ABP\\demo.db;");
});var app = builder.Build();
app.InitializeApplication();
app.Run();
4.4、修改appsettings.json,内容如下:
{"ConnectionStrings": {"conn_mssql": "Data Source=E:\\ABP\\demo.db;","Default": "Data Source=E:\\ABP\\demo.db;"},"Logging": {"LogLevel": {"Default": "Information","Microsoft": "Warning","Microsoft.Hosting.Lifetime": "Information"}},"AllowedHosts": "*"
}
5、数据迁移操作:
5.1、在E盘创建ABP文件夹,在ABP文件夹中创建demo.db数据库。sqlite数据库如何创建可以参考这个连接SQLite创建数据库 -SQLite教程。
5.2、将DemoAspNetCoreApplict 设置为启动项目,打开程序包管理器控制台,默认项目选择DemoEntityFrameworkCore。
5.3、程序包管理器控制台中输入Add-Migration init。
5.4、程序包管理器控制台中输入Update-Database。
6、问题:
6.1、大家可能注意到我的数据库连接字符串出现了很多次,在DemoAspNetCoreApplict项目中的Program.cs类中有,appsettings.json文件中有;有效的是Program.cs类中的。具体什么情况我还不太明白,有兴趣的大神请帮忙解答一下。
6.2、如果觉得一步步操作太累,还可以直接下载我上传的资源,链接如下:
https://download.csdn.net/download/xingchengaiwei/88795248