精简版Abp开发教程 - 第二章: 创建实体与数据库
我们基于官方教程并结合下列教程,开始创建Book之旅。
创建BookType枚举
在Acme.BookStore.Domain.Shared
中添加BookType.cs
namespace Acme.BookStore.Domain.Shared.Enums
{
public enum BookType
{
Undefined,
Adventure,
Biography,
Dystopia,
Fantastic,
Horror,
Science,
ScienceFiction,
Poetry
}
}
创建Book实体
在Acme.BookStore.Domain
中添加Book.cs
namespace Acme.BookStore.Domain.Entities
{
public class Book : AuditedAggregateRoot<Guid>
{
public string Name { get; set; }
public BookType Type { get; set; }
public DateTime PublishDate { get; set; }
public float Price { get; set; }
}
}
创建DbContext
在Acme.BookStore.EntityFrameworkCore
中添加BookStoreDbContext.cs
namespace Acme.BookStore.EntityFrameworkCore
{
[ConnectionStringName("Default")]
public class BookStoreDbContext : AbpDbContext<BookStoreDbContext>
{
public DbSet<Book> Books { get; set; }
public BookStoreDbContext(DbContextOptions<BookStoreDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//自动加载数据库映射配置
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
}
}
这个写法与官方教程不同,官方是
BookStoreDbContextModelCreatingExtensions
进行数据库映射配置
创建Book实体映射
在Acme.BookStore.EntityFrameworkCore
中添加BookMap.cs
namespace Acme.BookStore.EntityFrameworkCore.Mappings
{
public class BookMap : IEntityTypeConfiguration<Book>
{
public void Configure(EntityTypeBuilder<Book> builder)
{
builder.ToTable(nameof(Book));
builder.ConfigureByConvention();
builder.Property(x => x.Name).IsRequired().HasMaxLength(128);
}
}
}
创建Migrations DbContext Factory类
在Acme.BookStore.EntityFrameworkCore.DbMigrations
中添加BookStoreMigrationsDbContextFactory.cs
namespace Acme.BookStore.EntityFrameworkCore.DbMigrations
{
public class BookStoreMigrationsDbContextFactory : IDesignTimeDbContextFactory<BookStoreDbContext>
{
public BookStoreDbContext CreateDbContext(string[] args)
{
var configuration = BuildConfiguration();
var builder = new DbContextOptionsBuilder<BookStoreDbContext>()
.UseSqlServer(
configuration.GetConnectionString("Default"),
optionsBuilder => optionsBuilder.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName));
return new BookStoreDbContext(builder.Options);
}
private static IConfigurationRoot BuildConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);
return builder.Build();
}
}
}
添加appsettings.json
在Acme.BookStore.EntityFrameworkCore.DbMigrations
中添加appsettings.json
{
"ConnectionStrings": {
"Default": "Server=(LocalDb)\\MSSQLLocalDB;Database=Acme.BookStore;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
这个写法与官方教程不同,官方是以
.DbMigrator
或.Host
项目为启动项目,并且使用项目中的中appsettings.json
文件进行迁移操作。
我们这是直接以Acme.BookStore.EntityFrameworkCore.DbMigrations
为启动项目,进行迁移操作。
数据迁移
- 选择
Acme.BookStore.EntityFrameworkCore.DbMigrations
设为启动项目。 - 启动模板使用EF Core Code First Migrations创建和维护数据库架构. 打开菜单工具 > NuGet包管理器下的程序包管理控制台 (PMC).
- 选择
Acme.BookStore.EntityFrameworkCore.DbMigrations
做为默认项目然后执行以下命令:
Add-Migration "Created_Book_Entity"
- 更新数据库,执行以下命令:
Update-Database