精简版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为启动项目,进行迁移操作。

数据迁移

  1. 选择Acme.BookStore.EntityFrameworkCore.DbMigrations设为启动项目。
  2. 启动模板使用EF Core Code First Migrations创建和维护数据库架构. 打开菜单工具 > NuGet包管理器下的程序包管理控制台 (PMC).
  3. 选择 Acme.BookStore.EntityFrameworkCore.DbMigrations 做为默认项目然后执行以下命令:
Add-Migration "Created_Book_Entity"
  1. 更新数据库,执行以下命令:
Update-Database

至此Book实体及数据库表创建成功。

posted @ 2020-10-10 19:40  HUGO.CM  阅读(526)  评论(0编辑  收藏  举报