|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 62 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 12 KiB |
@ -0,0 +1,930 @@ |
|||||
|
|
||||
|
# EF Core数据库迁移 |
||||
|
|
||||
|
本文首先介绍[应用程序启动模板](Startup-Templates/Application.md)提供的**默认结构**,并讨论您可能希望为自己的应用程序实现的**各种场景**. |
||||
|
|
||||
|
> 本文档适用于希望完全理解和自定义[应用程序启动模板](Startup-Templates/Application.md)附带的数据库结构的人员. 如果你只是想创建实体和管理代码优先(code first)迁移,只需要遵循[启动教程](Tutorials/Index.md). |
||||
|
|
||||
|
### 源码 |
||||
|
|
||||
|
你可以在[这里](https://github.com/abpframework/abp/tree/dev/samples/EfCoreMigrationDemo)找到本文引用的示例项目的源代码; 但是为了理解示例项目的源代码,你需要阅读和理解这个文档. |
||||
|
|
||||
|
## 关于EF Core 代码优先迁移 |
||||
|
|
||||
|
Entity Framework Core 提供了一种简单强大[数据库迁移系统](https://docs.microsoft.com/zh-cn/ef/core/managing-schemas/migrations/). ABP框架[启动模板](Startup-Templates/Index.md)使用这个系统,让你以标准的方式开发你的应用程序. |
||||
|
|
||||
|
但是EF Core迁移系统在**模块化环境中不是很好**,在模块化环境中,每个模块都维护**自己的数据库模式**,而实际上两个或多个模块可以**共享一个数据库**. |
||||
|
|
||||
|
由于ABP框架在所有方面都关心模块化,所以它为这个问题提供了**解决方案**. 如果你需要**自定义数据库结构**,那么应当了解这个解决方案. |
||||
|
|
||||
|
> 参阅[EF Core文档](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/)充分了解EF Core Code First迁移,以及为什么需要这样的系统. |
||||
|
|
||||
|
## 默认解决方案与数据库配置 |
||||
|
|
||||
|
当你[创建一个新的Web应用程序](https://abp.io/get-started)(使用EF Core,它是默认的数据库提供程序),你的解决方案结构类似下图: |
||||
|
|
||||
|
 |
||||
|
|
||||
|
实际的解决方案结构可能会根据你的偏好有所不同,但是数据库部分是相同的. |
||||
|
|
||||
|
> 本文档将使用 `Acme.BookStore` 示例项目名称来引用项目和类. 你需要在解决方案中找到相应的类/项目. |
||||
|
|
||||
|
### 数据库模式 |
||||
|
|
||||
|
启动模板已预安装了一些[应用程序模块](Modules/Index.md). 解决方案的每一层都有相应的模块包引用. 所以 `.EntityFrameworkCore` 项目含有使用 `EntityFrameworkCore` 模块的Nuget的引用: |
||||
|
|
||||
|
 |
||||
|
|
||||
|
通过这种方式,你可以看到所有的 `.EntityFrameworkCore` 项目下的**EF Core的依赖**. |
||||
|
|
||||
|
> 除了模块引用之外,它还引用了 `Volo.Abp.EntityFrameworkCore.SqlServer` 包,因为启动模板预配置的是Sql Server. 参阅文档了解如何[切换到其它DBMS](Entity-Framework-Core-Other-DBMS.md). |
||||
|
|
||||
|
虽然每个模块在设计上有自己的 `DbContext` 类,并且可以使用其自己的**物理数据库**,但解决方案的配置是使用**单个共享数据库**如下图所示: |
||||
|
|
||||
|
 |
||||
|
|
||||
|
这是**最简单的配置**,适用于大部分的应用程序. `appsettings.json` 文件有名为`Default`**单个连接字符串**: |
||||
|
|
||||
|
````json |
||||
|
"ConnectionStrings": { |
||||
|
"Default": "..." |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
所以你有一个**单一的数据库模式**,其中包含**共享**此数据库的模块的所有表. |
||||
|
|
||||
|
ABP框架的[连接字符串](Connection-Strings.md)系统允许你轻松为所需的模块**设置不同的连接字符串**: |
||||
|
|
||||
|
````json |
||||
|
"ConnectionStrings": { |
||||
|
"Default": "...", |
||||
|
"AbpAuditLogging": "..." |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
示例配置告诉ABP框架[审计日志模块](Modules/Audit-Logging.md)应使用第二个连接字符串. |
||||
|
|
||||
|
**然而这仅仅只是开始**. 你还需要创建第二个数据库以及里面审计日志表并使用code frist迁移的方法维护数据库表. 本文档的主要目的之一就是指导你了解这样的数据库分离场景. |
||||
|
|
||||
|
#### 模块表 |
||||
|
|
||||
|
每个模块都使用**自己的数据库表**. 例如[身份模块](Modules/Identity.md)有一些表来管理系统中的用户和角色. |
||||
|
|
||||
|
##### 表前缀 |
||||
|
|
||||
|
由于所有模块都允许共享一个数据库(这是默认配置),所以模块通常使用**表前缀**来对自己的表进行分组. |
||||
|
|
||||
|
基础模块(如[身份](Modules/Identity.md), [租户管理](Modules/Tenant-Management.md) 和 [审计日志](Modules/Audit-Logging.md))使用 `Abp` 前缀, 其他的模块使用自己的前缀. 如[Identity Server](Modules/IdentityServer.md) 模块使用前缀 `IdentityServer`. |
||||
|
|
||||
|
如果你愿意,你可以为你的应用程序的模块**更改数据库表前缀**. |
||||
|
例: |
||||
|
|
||||
|
````csharp |
||||
|
Volo.Abp.IdentityServer.AbpIdentityServerDbProperties.DbTablePrefix = "Ids"; |
||||
|
```` |
||||
|
|
||||
|
这段代码更改了[Identity Server](Modules/IdentityServer.md)的前缀. 在应用程序的**最开始**编写这段代码. |
||||
|
|
||||
|
> 每个模块还定义了 `DbSchema` 属性,你可以在支持schema的数据库中使用它. |
||||
|
|
||||
|
### 项目 |
||||
|
|
||||
|
从数据库的角度来看.有三个重要的项目将在下一节中解释. |
||||
|
|
||||
|
#### .EntityFrameworkCore 项目 |
||||
|
|
||||
|
这个项目有应用程序的 `DbContext`类(本例中的 `BookStoreDbContex` ). |
||||
|
|
||||
|
**每个模块都使用自己的 `DbContext` 类**来访问数据库。同样你的应用程序有它自己的 `DbContext`. 通常在应用程序中使用这个 `DbContet`(如果你遵循最佳实践,应该在自定义[仓储](Repositories.md)中使用). 它几乎是一个空的 `DbContext`,因为你的应用程序在一开始没有任何实体,除了预定义的 `AppUser` 实体: |
||||
|
|
||||
|
````csharp |
||||
|
[ConnectionStringName("Default")] |
||||
|
public class BookStoreDbContext : AbpDbContext<BookStoreDbContext> |
||||
|
{ |
||||
|
public DbSet<AppUser> Users { get; set; } |
||||
|
|
||||
|
/* Add DbSet properties for your Aggregate Roots / Entities here. */ |
||||
|
|
||||
|
public BookStoreDbContext(DbContextOptions<BookStoreDbContext> options) |
||||
|
: base(options) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
protected override void OnModelCreating(ModelBuilder builder) |
||||
|
{ |
||||
|
base.OnModelCreating(builder); |
||||
|
|
||||
|
/* Configure the shared tables (with included modules) here */ |
||||
|
|
||||
|
builder.Entity<AppUser>(b => |
||||
|
{ |
||||
|
//Sharing the same table "AbpUsers" with the IdentityUser |
||||
|
b.ToTable("AbpUsers"); |
||||
|
|
||||
|
//Configure base properties |
||||
|
b.ConfigureByConvention(); |
||||
|
b.ConfigureAbpUser(); |
||||
|
|
||||
|
//Moved customization of the "AbpUsers" table to an extension method |
||||
|
b.ConfigureCustomUserProperties(); |
||||
|
}); |
||||
|
|
||||
|
/* Configure your own tables/entities inside the ConfigureBookStore method */ |
||||
|
builder.ConfigureBookStore(); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
这个简单的 `DbContext` 类仍然需要一些解释: |
||||
|
|
||||
|
* 它定义了一个 `[connectionStringName]` Attribute,它告诉ABP始终为此 `Dbcontext` 使用 `Default` 连接字符串. |
||||
|
* 它从 `AbpDbContext<T>` 而不是标准的 `DbContext` 类继承. 你可以参阅[EF Core集成](Entity-Framework-Core.md)文档了解更多. 现在你需要知道 `AbpDbContext<T>` 基类实现ABP框架的一些约定,为你自动化一些常见的任务. |
||||
|
* 它为 `AppUser` 实体定义了 `DbSet` 属性. `AppUser` 与[身份模块](Modules/Identity.md)的 `IdentityUser` 实体共享同一个表(默认名为 `AbpUsers`). 启动模板在应用程序中提供这个实体,因为我们认为用户实体一般需要应用程序中进行定制. |
||||
|
* 构造函数接受一个 `DbContextOptions<T>` 实例. |
||||
|
* 它覆盖了 `OnModelCreating` 方法定义EF Core 映射. |
||||
|
* 首先调用 `base.OnModelCreating` 方法让ABP框架为我们实现基础映射. |
||||
|
* 然后它配置了 `AppUser` 实体的映射. 这个实体有一个特殊的情况(它与Identity模块共享一个表),在下一节中进行解释. |
||||
|
* 最后它调用 `builder.ConfigureBookStore()` 扩展方法来配置应用程序的其他实体. |
||||
|
|
||||
|
在介绍其他数据库相关项目之后,将更详细地说明这个设计. |
||||
|
|
||||
|
#### .EntityFrameworkCore.DbMigrations 项目 |
||||
|
|
||||
|
正如前面所提到的,每个模块(和你的应用程序)有**它们自己**独立的 `DbContext` 类. 每个 `DbContext` 类只定义了自身模块的实体到表的映射,每个模块(包括你的应用程序)在**运行时**都使用相关的 `DbContext` 类. |
||||
|
|
||||
|
如你所知,EF Core Code First迁移系统依赖于 `DbContext` 类来跟踪和生成Code First迁移. 那么我们应该使用哪个 `DbContext` 进行迁移? 答案是它们都不是. `.EntityFrameworkCore.DbMigrations` 项目中定义了另一个 `DbContext` (示例解决方案中的 `BookStoreMigrationsDbContext`). |
||||
|
|
||||
|
##### MigrationsDbContext |
||||
|
|
||||
|
`MigrationsDbContext` 仅用于创建和应用数据库迁移. **不在运行时使用**. 它将所有使用的模块的所有实体到表的映射以及应用程序的映射**合并**. |
||||
|
|
||||
|
通过这种方式你可以创建和维护**单个数据库迁移路径**. 然而这种方法有一些困难,接下来的章节将解释ABP框架如何克服这些困难. 首先以 `BookStoreMigrationsDbContext` 类为例: |
||||
|
|
||||
|
````csharp |
||||
|
/* This DbContext is only used for database migrations. |
||||
|
* It is not used on runtime. See BookStoreDbContext for the runtime DbContext. |
||||
|
* It is a unified model that includes configuration for |
||||
|
* all used modules and your application. |
||||
|
*/ |
||||
|
public class BookStoreMigrationsDbContext : AbpDbContext<BookStoreMigrationsDbContext> |
||||
|
{ |
||||
|
public BookStoreMigrationsDbContext( |
||||
|
DbContextOptions<BookStoreMigrationsDbContext> options) |
||||
|
: base(options) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
protected override void OnModelCreating(ModelBuilder builder) |
||||
|
{ |
||||
|
base.OnModelCreating(builder); |
||||
|
|
||||
|
/* Include modules to your migration db context */ |
||||
|
builder.ConfigurePermissionManagement(); |
||||
|
builder.ConfigureSettingManagement(); |
||||
|
builder.ConfigureBackgroundJobs(); |
||||
|
builder.ConfigureAuditLogging(); |
||||
|
builder.ConfigureIdentity(); |
||||
|
builder.ConfigureIdentityServer(); |
||||
|
builder.ConfigureFeatureManagement(); |
||||
|
builder.ConfigureTenantManagement(); |
||||
|
|
||||
|
/* Configure customizations for entities from the modules included */ |
||||
|
builder.Entity<IdentityUser>(b => |
||||
|
{ |
||||
|
b.ConfigureCustomUserProperties(); |
||||
|
}); |
||||
|
|
||||
|
/* Configure your own tables/entities inside the ConfigureBookStore method */ |
||||
|
builder.ConfigureBookStore(); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
##### 共享映射代码 |
||||
|
|
||||
|
第一个问题是: 一个模块使用自己的 `DbContext` 这就需要到数据库的映射. 该 `MigrationsDbContext` 也需要相同的映射创建此模块的数据库表. 我们绝对**不希望复制**的映射代码. |
||||
|
|
||||
|
解决方案是定义一个**扩展方法**(在`ModelBuilder`)由两个 `DbContext` 类调用. 所以每个模块都定义了这样的扩展方法. |
||||
|
|
||||
|
例如 `builder.ConfigureBackgroundJobs()` 方法调用[后台作业模块](Modules/Background-Jobs.md)配置数据库表. 扩展方法的定义如下: |
||||
|
|
||||
|
````csharp |
||||
|
public static class BackgroundJobsDbContextModelCreatingExtensions |
||||
|
{ |
||||
|
public static void ConfigureBackgroundJobs( |
||||
|
this ModelBuilder builder, |
||||
|
Action<BackgroundJobsModelBuilderConfigurationOptions> optionsAction = null) |
||||
|
{ |
||||
|
var options = new BackgroundJobsModelBuilderConfigurationOptions( |
||||
|
BackgroundJobsDbProperties.DbTablePrefix, |
||||
|
BackgroundJobsDbProperties.DbSchema |
||||
|
); |
||||
|
|
||||
|
optionsAction?.Invoke(options); |
||||
|
|
||||
|
builder.Entity<BackgroundJobRecord>(b => |
||||
|
{ |
||||
|
b.ToTable(options.TablePrefix + "BackgroundJobs", options.Schema); |
||||
|
|
||||
|
b.ConfigureCreationTime(); |
||||
|
b.ConfigureExtraProperties(); |
||||
|
|
||||
|
b.Property(x => x.JobName) |
||||
|
.IsRequired() |
||||
|
.HasMaxLength(BackgroundJobRecordConsts.MaxJobNameLength); |
||||
|
|
||||
|
//... |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
此u还获取选项用于更改此模块的数据库表前缀和模式,但在这里并不重要. |
||||
|
|
||||
|
最终的应用程序在 `MigrationsDbContext` 类中调用扩展方法, 因此它可以确定此 `MigrationsDbContext` 维护的数据库中包含哪些模块. 如果要创建第二个数据库并将某些模块表移动到第二个数据库,则需要有第二个`MigrationsDbContext` 类,该类仅调用相关模块的扩展方法. 下一部分将详细介绍该主题. |
||||
|
|
||||
|
同样 `ConfigureBackgroundJobs` 方法也被后台作业模块的 `DbContext` 调用: |
||||
|
|
||||
|
````csharp |
||||
|
[ConnectionStringName(BackgroundJobsDbProperties.ConnectionStringName)] |
||||
|
public class BackgroundJobsDbContext |
||||
|
: AbpDbContext<BackgroundJobsDbContext>, IBackgroundJobsDbContext |
||||
|
{ |
||||
|
public DbSet<BackgroundJobRecord> BackgroundJobs { get; set; } |
||||
|
|
||||
|
public BackgroundJobsDbContext(DbContextOptions<BackgroundJobsDbContext> options) |
||||
|
: base(options) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
protected override void OnModelCreating(ModelBuilder builder) |
||||
|
{ |
||||
|
base.OnModelCreating(builder); |
||||
|
|
||||
|
//Reuse the same extension method! |
||||
|
builder.ConfigureBackgroundJobs(); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
通过这种方式,可以在 `DbContext` 类之间共享模块的映射配置. 上面的代码在相关的模块NuGet包中,所以你不需要关心它. |
||||
|
|
||||
|
##### 重用模块的表 |
||||
|
|
||||
|
您可能想在应用程序中**重用依赖模块的表**. 在这种情况下你有两个选择: |
||||
|
|
||||
|
1. 你可以**直接使用模块定义的实体**. |
||||
|
2. 你可以**创建一个新的实体**映射到同一个数据库表。 |
||||
|
|
||||
|
###### 使用由模块定义的实体 |
||||
|
|
||||
|
使用实体定义的模块有标准用法非常简单. 例如身份模块定义了 `IdentityUser` 实体. 你可以为注入 `IdentityUser` 仓储,为此实体执行标准仓储操作. |
||||
|
例: |
||||
|
|
||||
|
````csharp |
||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Domain.Repositories; |
||||
|
using Volo.Abp.Identity; |
||||
|
|
||||
|
namespace Acme.BookStore |
||||
|
{ |
||||
|
public class MyService : ITransientDependency |
||||
|
{ |
||||
|
private readonly IRepository<IdentityUser, Guid> _identityUserRepository; |
||||
|
|
||||
|
public MyService(IRepository<IdentityUser, Guid> identityUserRepository) |
||||
|
{ |
||||
|
_identityUserRepository = identityUserRepository; |
||||
|
} |
||||
|
|
||||
|
public async Task DoItAsync() |
||||
|
{ |
||||
|
//Get all users |
||||
|
var users = await _identityUserRepository.GetListAsync(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
示例注入了 `IRepository<IdentityUser,Guid>`(默认仓储). 它定义了标准的存储库方法并实现了 `IQueryable` 接口. |
||||
|
|
||||
|
另外,身份模块定义了 `IIdentityUserRepository`(自定义仓储),你的应用程序也可以注入和使用它. `IIdentityUserRepository` 为 `IdentityUser` 实体提供了额外的定制方法,但它没有实现 `IQueryable`. |
||||
|
|
||||
|
###### 创建一个新的实体 |
||||
|
|
||||
|
如果你只是按原样使用实体,那么使用模块的实体是很容易的. 但是在下面的情况下你可能需要定义自己的实体并映射到模块实体相同的数据库表. |
||||
|
|
||||
|
* 你想要**添加一个新的字段**到表中并将其映射到实体的属性. 你无法使用模块的实体,因为它没有相关的属性. |
||||
|
* 你想要**使用的表字段的一个子集**. 你不希望访问实体的所有属性并隐藏不相关的属性(从安全的角度或仅出于设计目的). |
||||
|
* 你不希望直接**依赖于**模块实体类. |
||||
|
|
||||
|
无论哪一种情况,流程都是一样的. 假如你想要创建一个名为 `AppRole` 的实体,将其映射到与[身份模块](Modules/Identity.md)的 `IdentityRole` 实体相同的表 |
||||
|
|
||||
|
在这里我们将介绍实现方式, 然后**将讨论这种方法的局限性**. |
||||
|
|
||||
|
首先,在你的 `.Domain` 项目一个新的 `AppRole` 类: |
||||
|
|
||||
|
````csharp |
||||
|
using System; |
||||
|
using Volo.Abp.Domain.Entities; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
|
||||
|
namespace Acme.BookStore.Roles |
||||
|
{ |
||||
|
public class AppRole : AggregateRoot<Guid>, IMultiTenant |
||||
|
{ |
||||
|
// Properties shared with the IdentityRole class |
||||
|
|
||||
|
public Guid? TenantId { get; private set; } |
||||
|
public string Name { get; private set; } |
||||
|
|
||||
|
//Additional properties |
||||
|
|
||||
|
public string Title { get; set; } |
||||
|
|
||||
|
private AppRole() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
* 它继承了[`AggregateRoot<Guid>`类](Entities.md)和实现了[`IMultiTenant`]接口(Multi-Tenancy.md),因为 `IdentityRole` 也做了同样的继承. |
||||
|
* 你可以添加 `IdentityRole` 实体定义的任何属性. 本例只加了 `TenantId` 和 `Name` 属性,因为我们这里只需要它们. 你可以把setters设置为私有(如同本例)以防意外更改身份模块的属性. |
||||
|
* 你可以添加自定义(附加)属性. 本例添加了 `Title` 属性. |
||||
|
* **构造函数是私有的**,所以它不允许直接创建一个新的 `AppRole` 实体。创建角色身份模块的责任. 你可以查询角色,设置/更新自定义属性,但做为最佳实践你不应该在代码中创建和删除角色(尽管没有强制的限制). |
||||
|
|
||||
|
现在是时候定义EF Core映射. 打开应用程序的 `DbContext` (此示例中是 `BookStoreDbContext` )添加以下属性: |
||||
|
|
||||
|
````csharp |
||||
|
public DbSet<AppRole> Roles { get; set; } |
||||
|
```` |
||||
|
|
||||
|
然后在 `OnModelCreating` 方法中配置映射(调用 `base.OnModelCreating(builder)` 之后): |
||||
|
|
||||
|
````csharp |
||||
|
protected override void OnModelCreating(ModelBuilder builder) |
||||
|
{ |
||||
|
base.OnModelCreating(builder); |
||||
|
|
||||
|
/* Configure the shared tables (with included modules) here */ |
||||
|
|
||||
|
//CONFIGURE THE AppRole ENTITY |
||||
|
builder.Entity<AppRole>(b => |
||||
|
{ |
||||
|
b.ToTable("AbpRoles"); |
||||
|
|
||||
|
b.ConfigureByConvention(); |
||||
|
|
||||
|
b.ConfigureCustomRoleProperties(); |
||||
|
}); |
||||
|
|
||||
|
... |
||||
|
|
||||
|
/* Configure your own tables/entities inside the ConfigureBookStore method */ |
||||
|
|
||||
|
builder.ConfigureBookStore(); |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
我们添加了以下几行: |
||||
|
|
||||
|
````csharp |
||||
|
builder.Entity<AppRole>(b => |
||||
|
{ |
||||
|
b.ToTable("AbpRoles"); |
||||
|
|
||||
|
b.ConfigureByConvention(); |
||||
|
|
||||
|
b.ConfigureCustomRoleProperties(); |
||||
|
}); |
||||
|
```` |
||||
|
|
||||
|
* 它映射到 `AbpRoles` 表,与 `IdentityRole` 实体共享. |
||||
|
* `ConfigureByConvention()` 配置了标准/基本属性(像`TenantId`),建议总是调用它. |
||||
|
|
||||
|
`ConfigureCustomRoleProperties()` 还不存在. 在 `BookStoreDbContextModelCreatingExtensions` 类中定义它 (在 `.EntityFrameworkCore` 项目的 `DbContext` 附近): |
||||
|
|
||||
|
````csharp |
||||
|
public static void ConfigureCustomRoleProperties<TRole>(this EntityTypeBuilder<TRole> b) |
||||
|
where TRole : class, IEntity<Guid> |
||||
|
{ |
||||
|
b.Property<string>(nameof(AppRole.Title)).HasMaxLength(128); |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
* 这个方法只定义实体的**自定义属性**. |
||||
|
* 遗憾的是,我们不能在这里充分的利用**类型安全**(通过引用`AppRole`实体). 我们能做的最好就是使用 `Title` 名称做为类型安全。 |
||||
|
|
||||
|
你已经为运行应用程序使用的 `DbContext` 配置了自定义属性. 我们还需要配置 `MigrationsDbContext`. |
||||
|
|
||||
|
打开`MigrationsDbContext`(本例是 `BookStoreMigrationsDbContext`)进行以下更改: |
||||
|
|
||||
|
````csharp |
||||
|
protected override void OnModelCreating(ModelBuilder builder) |
||||
|
{ |
||||
|
base.OnModelCreating(builder); |
||||
|
|
||||
|
/* Include modules to your migration db context */ |
||||
|
|
||||
|
... |
||||
|
|
||||
|
/* Configure customizations for entities from the modules included */ |
||||
|
|
||||
|
//CONFIGURE THE CUSTOM ROLE PROPERTIES |
||||
|
builder.Entity<IdentityRole>(b => |
||||
|
{ |
||||
|
b.ConfigureCustomRoleProperties(); |
||||
|
}); |
||||
|
|
||||
|
... |
||||
|
|
||||
|
/* Configure your own tables/entities inside the ConfigureBookStore method */ |
||||
|
|
||||
|
builder.ConfigureBookStore(); |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
只增加下面几行: |
||||
|
|
||||
|
````csharp |
||||
|
builder.Entity<IdentityRole>(b => |
||||
|
{ |
||||
|
b.ConfigureCustomRoleProperties(); |
||||
|
}); |
||||
|
```` |
||||
|
|
||||
|
通过这种方式,我们重用了用于为角色配置自定义属性映射的扩展方法. 但是对 `IdentityRole` 实体进行了相同的自定义. |
||||
|
|
||||
|
现在你可以在包管理控制台(记得选择 `.EntityFrameworkCore.DbMigrations` 做为PMC的默认项目并将 `.Web` 项目设置为启动项目)使用标准的 `Add-Migration` 命令添加一个新的EF Core数据库迁移. |
||||
|
|
||||
|
 |
||||
|
|
||||
|
命令将创建一个新的Code First迁移类,如下所示: |
||||
|
|
||||
|
````csharp |
||||
|
public partial class Added_Title_To_Roles : Migration |
||||
|
{ |
||||
|
protected override void Up(MigrationBuilder migrationBuilder) |
||||
|
{ |
||||
|
migrationBuilder.AddColumn<string>( |
||||
|
name: "Title", |
||||
|
table: "AbpRoles", |
||||
|
maxLength: 128, |
||||
|
nullable: true); |
||||
|
} |
||||
|
|
||||
|
protected override void Down(MigrationBuilder migrationBuilder) |
||||
|
{ |
||||
|
migrationBuilder.DropColumn( |
||||
|
name: "Title", |
||||
|
table: "AbpRoles"); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
全部完成,只运行在PMC运行`Update-Database` 命令或运行你解决方案中的`.DbMigrator` 项目应用数据库更改. |
||||
|
|
||||
|
现在你可以像使用应用程序的其他实体一样使用 `AppRole` 实体. 下面是查询和更新角色的[应用服务](Application-Services.md). |
||||
|
示例: |
||||
|
|
||||
|
````csharp |
||||
|
public class AppRoleAppService : ApplicationService, IAppRoleAppService |
||||
|
{ |
||||
|
private readonly IRepository<AppRole, Guid> _appRoleRepository; |
||||
|
|
||||
|
public AppRoleAppService(IRepository<AppRole, Guid> appRoleRepository) |
||||
|
{ |
||||
|
_appRoleRepository = appRoleRepository; |
||||
|
} |
||||
|
|
||||
|
public async Task<List<AppRoleDto>> GetListAsync() |
||||
|
{ |
||||
|
var roles = await _appRoleRepository.GetListAsync(); |
||||
|
|
||||
|
return roles |
||||
|
.Select(r => new AppRoleDto |
||||
|
{ |
||||
|
Id = r.Id, |
||||
|
Name = r.Name, |
||||
|
Title = r.Title |
||||
|
}) |
||||
|
.ToList(); |
||||
|
} |
||||
|
|
||||
|
public async Task UpdateTitleAsync(Guid id, string title) |
||||
|
{ |
||||
|
var role = await _appRoleRepository.GetAsync(id); |
||||
|
|
||||
|
role.Title = title; |
||||
|
|
||||
|
await _appRoleRepository.UpdateAsync(role); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
创建新实体并将其映射到依赖模块的表存在一些**限制**: |
||||
|
|
||||
|
* 你的**自定义属性必须是可空的**. 例如 `AppRole.Title` 在这里是可空的. 否则身份模块会抛出异常,因为它在向数据库插入新角色时不知道如何如何填充标题. |
||||
|
* 好的做法是,你不应更新**模块定义的属性**,尤其是在需要业务逻辑的情况下. 通常你可以管理自己(自定义)的属性. |
||||
|
|
||||
|
##### 替代方法 |
||||
|
|
||||
|
你可以使用以下方法替代创建实体添加自定义属性. |
||||
|
|
||||
|
###### 使用ExtraProperties |
||||
|
|
||||
|
所有从 `AggregateRoot` 派生的实体都可以在 `ExtraProperties` 属性中存储键值对, 它是 `Dictionary<string, object>` 类型在数据库中被序列化为JSON. 所以你可以在字典中添加值用于查询,无需更改实体. |
||||
|
|
||||
|
例如你可以将查询属性 `Title` 存储在 `IdentityRole` 中,而不是创建一个新的实体. |
||||
|
例: |
||||
|
|
||||
|
````csharp |
||||
|
public class IdentityRoleExtendingService : ITransientDependency |
||||
|
{ |
||||
|
private readonly IIdentityRoleRepository _identityRoleRepository; |
||||
|
|
||||
|
public IdentityRoleExtendingService(IIdentityRoleRepository identityRoleRepository) |
||||
|
{ |
||||
|
_identityRoleRepository = identityRoleRepository; |
||||
|
} |
||||
|
|
||||
|
public async Task<string> GetTitleAsync(Guid id) |
||||
|
{ |
||||
|
var role = await _identityRoleRepository.GetAsync(id); |
||||
|
|
||||
|
return role.GetProperty<string>("Title"); |
||||
|
} |
||||
|
|
||||
|
public async Task SetTitleAsync(Guid id, string newTitle) |
||||
|
{ |
||||
|
var role = await _identityRoleRepository.GetAsync(id); |
||||
|
|
||||
|
role.SetProperty("Title", newTitle); |
||||
|
|
||||
|
await _identityRoleRepository.UpdateAsync(role); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
* `GetProperty` 和 `SetProperty` 方法是在 `role.ExtraProperties` 词典中获取和设置值的简化写法,推荐使用. |
||||
|
|
||||
|
通过这种方式你可以轻松任何类型的值附加到依赖模块的实体. 不过这种方法也有一些缺点: |
||||
|
|
||||
|
* 所有的额外属性都存储在数据库中的一个**JSON对象**,它们不是作为表的字段存储,与简单的表字段相比创建索引和针对此属性使用SQL查询将更加困难. |
||||
|
* 属性名称是字符串,他们**不是类型安全的**. 建议这些类型的属性定义常量,以防止拼写错误. |
||||
|
|
||||
|
###### 创建新表 |
||||
|
|
||||
|
你可以创建**自己的表**来存储属性,而不是创建新实体并映射到同一表. 你通常复制原始实体的一些值. 例如可以将 `Name` 字段添加到你自己的表中,它是原表中 `Name` 字段的副本. |
||||
|
|
||||
|
在这种情况下你不需要处理迁移问题,但是需要处理数据复制问题. 当重复的值发生变化时,你应该在表中同步相同的变化. 你可以使用本地或分布式[事件总线](Event-Bus.md)订阅原始实体的更改事件. 这是根据来自另一个微服务的数据推荐的方法,特别是如果它们有单独的物理数据库(你可以在网络中搜索关于微服务设计的数据共享,这是一个广泛的主题). |
||||
|
|
||||
|
#### 讨论另一种场景:每个模块管理自己的迁移路径 |
||||
|
|
||||
|
上面提到, `.EntityFrameworkCore.DbMigrations` 合并所有模块的所有数据库映射(加上应用程序映射)创建统一的迁移路径. |
||||
|
|
||||
|
另一种方法是允许每个模块拥有自己的迁移来维护其数据库表. 虽然它在一开始看起来更模块化,但它有一些重要的缺点: |
||||
|
|
||||
|
* **EF Core迁移系统依赖于数据库DBMS提供程序**. 例如,如果一个模块为SQL Server创建迁移,那么你就不能使用MySQL使用这个迁移代码,对于模块而言为所有可用的DBMS提供程序维护迁移是不切实际的. 将迁移留给应用程序代码(如本文档中所解释的),你就可以在**应用程序代码中选择DBMS**. |
||||
|
* 在模块之间 **共享表** 或在应用程序中 **重用模块的表** 会更加困难. 因为EF Core迁移系统不能处理它,并会抛出异常,例如"数据库中已存在表XXX". |
||||
|
* **自定义/增强** 映射和生成迁移代码会更加困难. |
||||
|
* 当你使用多个模块时更难跟踪和**应用数据库更改**. |
||||
|
|
||||
|
## 使用多个数据库 |
||||
|
|
||||
|
默认的启动模板被组织为所有模块和应用程序使用单个数据库. 但是ABP框架和所有预构建的模块都被设计为**可以使用多个数据库**. 每个模块可以使用自己的数据库,也可以将模块分组到几个数据库中. |
||||
|
|
||||
|
本节将说明如何将审计日志,设置管理和权限管理模块表移至**第二个数据库**,而其余模块继续使用主("Default")数据库. |
||||
|
|
||||
|
结构如下图所示: |
||||
|
|
||||
|
 |
||||
|
|
||||
|
### 更改连接字符串部分 |
||||
|
|
||||
|
第一步是更改所有 `appsettings.json` 文件的连接字符串部分. 最初它是这样的: |
||||
|
|
||||
|
````json |
||||
|
"ConnectionStrings": { |
||||
|
"Default": "Server=localhost;Database=BookStore;Trusted_Connection=True;MultipleActiveResultSets=true" |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
更改如下所示: |
||||
|
|
||||
|
````json |
||||
|
"ConnectionStrings": { |
||||
|
"Default": "Server=localhost;Database=BookStore;Trusted_Connection=True;MultipleActiveResultSets=true", |
||||
|
"AbpPermissionManagement": "Server=localhost;Database=BookStore_SecondDb;Trusted_Connection=True;MultipleActiveResultSets=true", |
||||
|
"AbpSettingManagement": "Server=localhost;Database=BookStore_SecondDb;Trusted_Connection=True;MultipleActiveResultSets=true", |
||||
|
"AbpAuditLogging": "Server=localhost;Database=BookStore_SecondDb;Trusted_Connection=True;MultipleActiveResultSets=true" |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
为相关模块添加了**另外三个连接字符串**,以针对 `BookStore_SecondDb` 数据库(它们都是相同的). 例如 `AbpPermissionManagement` 是权限管理模块的连接字符串. |
||||
|
|
||||
|
`AbpPermissionManagement` 是权限管理模块[定义](https://github.com/abpframework/abp/blob/97eaa6ff5a044f503465455c86332e5a277b077a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/AbpPermissionManagementDbProperties.cs#L11)的一个常量. 如果定义值,ABP框架[连接字符串选择系统](Connection-Strings.md)将为权限管理模块选择此连接字符串. 如果不定义它将回退到 `Default` 连接字符串. |
||||
|
|
||||
|
### 创建第二个迁移项目 |
||||
|
|
||||
|
在**运行时**上定义如上所述的连接字符串就足够了. 但是 `BookStore_SecondDb` 数据库还不存在. 你需要为相关模块创建数据库表. |
||||
|
|
||||
|
就像主数据库我们要使用的EF Core Code Frist迁移系统创建和维护第二个数据库. |
||||
|
|
||||
|
一种简单的方法是为第二个迁移 `DbContext` 创建第二个项目(`.csproj`). |
||||
|
|
||||
|
在你的解决方案中创建一个名为 `Acme.BookStore.EntityFrameworkCore.DbMigrationsForSecondDb` 的**类库项目**(如果不喜欢你可以进行其他命名). |
||||
|
|
||||
|
`.csproj` 内容应该和以下类似: |
||||
|
|
||||
|
````xml |
||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<Import Project="..\..\common.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp3.1</TargetFramework> |
||||
|
<RootNamespace>Acme.BookStore.DbMigrationsForSecondDb</RootNamespace> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\Acme.BookStore.EntityFrameworkCore\Acme.BookStore.EntityFrameworkCore.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
|
```` |
||||
|
|
||||
|
你可以仅复制和修改原始 `.DbMigrations` 项目的内容. 该项目引用 `.EntityFrameworkCore` 项目. **唯一的区别**是 `RootNamespace` 值. |
||||
|
|
||||
|
从 `.Web` 项目中添加对该项目的引用(否则,EF Core Tool不允许使用`Add-migration`命令). |
||||
|
|
||||
|
### 创建第二个DbMigrationDbContext |
||||
|
|
||||
|
为迁移创建一个新的 `DbContext` 并调用模块的**扩展方法**为相关模块配置数据库表: |
||||
|
|
||||
|
````csharp |
||||
|
[ConnectionStringName("AbpPermissionManagement")] |
||||
|
public class BookStoreSecondMigrationsDbContext : |
||||
|
AbpDbContext<BookStoreSecondMigrationsDbContext> |
||||
|
{ |
||||
|
public BookStoreSecondMigrationsDbContext( |
||||
|
DbContextOptions<BookStoreSecondMigrationsDbContext> options) |
||||
|
: base(options) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
protected override void OnModelCreating(ModelBuilder builder) |
||||
|
{ |
||||
|
base.OnModelCreating(builder); |
||||
|
|
||||
|
/* Include modules to your migration db context */ |
||||
|
|
||||
|
builder.ConfigurePermissionManagement(); |
||||
|
builder.ConfigureSettingManagement(); |
||||
|
builder.ConfigureAuditLogging(); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
> `[ConnectionStringName(...)` Attribute 在这里非常重,它告诉ABP框架应该为这个 `DbContext` 使用哪个连接字符串. 我们使用了 `AbpPermissionManagement`. |
||||
|
|
||||
|
创建一个**Design Time Db Factory**类, EF Core Tool需要使用它(例如`Add-Migration`和`Update-Database` PCM命令): |
||||
|
|
||||
|
````csharp |
||||
|
/* This class is needed for EF Core console commands |
||||
|
* (like Add-Migration and Update-Database commands) */ |
||||
|
public class BookStoreSecondMigrationsDbContextFactory |
||||
|
: IDesignTimeDbContextFactory<BookStoreSecondMigrationsDbContext> |
||||
|
{ |
||||
|
public BookStoreSecondMigrationsDbContext CreateDbContext(string[] args) |
||||
|
{ |
||||
|
var configuration = BuildConfiguration(); |
||||
|
|
||||
|
var builder = new DbContextOptionsBuilder<BookStoreSecondMigrationsDbContext>() |
||||
|
.UseSqlServer(configuration.GetConnectionString("AbpPermissionManagement")); |
||||
|
|
||||
|
return new BookStoreSecondMigrationsDbContext(builder.Options); |
||||
|
} |
||||
|
|
||||
|
private static IConfigurationRoot BuildConfiguration() |
||||
|
{ |
||||
|
var builder = new ConfigurationBuilder() |
||||
|
.SetBasePath(Directory.GetCurrentDirectory()) |
||||
|
.AddJsonFile("appsettings.json", optional: false); |
||||
|
|
||||
|
return builder.Build(); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
它与 `.EntityFrameworCore.DbMigrations` 项目中的类相似, 除了它使用 `AbpPermissionManagement` 连接字符串. |
||||
|
|
||||
|
现在你可以打开包管理器控制台, 选择 `.EntityFrameworkCore.DbMigrationsForSecondDb` 项目为默认项目(确保`.Web`项目是启动项目),运行 `Add-Migration "Initial` 和 `Update-Database` 命令,如下所示: |
||||
|
|
||||
|
 |
||||
|
|
||||
|
现在你应该有一个包含相关数据库表的新数据库. |
||||
|
|
||||
|
 |
||||
|
|
||||
|
### 从主数据库中删除模块 |
||||
|
|
||||
|
我们 **创建第二个数据库** 包含了审计日志,权限管理和设置管理模块表. 我们应该从**主数据库中删除这些表**,这是很容易的. |
||||
|
S |
||||
|
首先移除从 `MigrationsDbContext` 类以下行(本例中是`BookStoreMigrationsDbContext`): |
||||
|
|
||||
|
````csharp |
||||
|
builder.ConfigurePermissionManagement(); |
||||
|
builder.ConfigureSettingManagement(); |
||||
|
builder.ConfigureAuditLogging(); |
||||
|
```` |
||||
|
|
||||
|
打开包管理器控制台, 选择 `.EntityFrameworkCore.DbMigrations` 为默认的项目(确保`.Web`项目是启动项目)运行以下命令: |
||||
|
|
||||
|
```` |
||||
|
Add-Migration "Removed_Audit_Setting_Permission_Modules" |
||||
|
```` |
||||
|
|
||||
|
此命令将创建一个如下所示的迁移类: |
||||
|
|
||||
|
````csharp |
||||
|
public partial class Removed_Audit_Setting_Permission_Modules : Migration |
||||
|
{ |
||||
|
protected override void Up(MigrationBuilder migrationBuilder) |
||||
|
{ |
||||
|
migrationBuilder.DropTable( |
||||
|
name: "AbpAuditLogActions"); |
||||
|
|
||||
|
migrationBuilder.DropTable( |
||||
|
name: "AbpEntityPropertyChanges"); |
||||
|
|
||||
|
migrationBuilder.DropTable( |
||||
|
name: "AbpPermissionGrants"); |
||||
|
|
||||
|
migrationBuilder.DropTable( |
||||
|
name: "AbpSettings"); |
||||
|
|
||||
|
migrationBuilder.DropTable( |
||||
|
name: "AbpEntityChanges"); |
||||
|
|
||||
|
migrationBuilder.DropTable( |
||||
|
name: "AbpAuditLogs"); |
||||
|
} |
||||
|
|
||||
|
... |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
在这一步需要小心: |
||||
|
|
||||
|
* 如果你是 **已经在生产的系统**, 那么你应该关心**数据丢失**,在删除表之前需要将表的内容移动到第二个数据库. |
||||
|
* 如果你还 **没有开始** 你的项目, 你可以考虑 **删除所有的迁移** 并且重新生成初始版本,让迁移历史更加清晰. |
||||
|
|
||||
|
运行 `Update-Database` 命令从主数据库中删除表. |
||||
|
|
||||
|
请注意如果你还没有将其复制到新数据库,那么还会**删除了一些初始种子数据**(例如授予管理员角色的权限). 如果你运行应用程序可能无法再登录. 解决方案很简单:在解决方案中重新运行 `.DbMigrator` 控制台应用程序,它会为新数据库添加种子数据. |
||||
|
|
||||
|
### 自动化第二个数据库模式迁移 |
||||
|
|
||||
|
`.DbMigrator` 控制台应用程序可以跨多个数据库运行数据库种子代码无需任何其他配置. 但它不能在第二个数据库迁移项目中运行EF Core First迁移. 下面介绍如何配置控制台迁移应用程序以处理两个数据库. |
||||
|
|
||||
|
#### 实现 IBookStoreDbSchemaMigrator |
||||
|
|
||||
|
`Acme.BookStore.EntityFrameworkCore.DbMigrations` 项目中的 `EntityFrameworkCoreBookStoreDbSchemaMigrator` 类负责为 `BookStoreMigrationsDbContext` 迁移数据库模式. 它应该是这样的: |
||||
|
|
||||
|
````csharp |
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class EntityFrameworkCoreBookStoreDbSchemaMigrator |
||||
|
: IBookStoreDbSchemaMigrator, ITransientDependency |
||||
|
{ |
||||
|
private readonly IServiceProvider _serviceProvider; |
||||
|
|
||||
|
public EntityFrameworkCoreBookStoreDbSchemaMigrator( |
||||
|
IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
_serviceProvider = serviceProvider; |
||||
|
} |
||||
|
|
||||
|
public async Task MigrateAsync() |
||||
|
{ |
||||
|
/* We are intentionally resolving the BookStoreMigrationsDbContext |
||||
|
* from IServiceProvider (instead of directly injecting it) |
||||
|
* to properly get the connection string of the current tenant in the |
||||
|
* current scope. |
||||
|
*/ |
||||
|
|
||||
|
await _serviceProvider |
||||
|
.GetRequiredService<BookStoreMigrationsDbContext>() |
||||
|
.Database |
||||
|
.MigrateAsync(); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
它实现了 `IBookStoreDbSchemaMigrator` 并**替换现有服务**(见第一行). |
||||
|
|
||||
|
删除 `[Dependency(ReplaceServices = true)]`,因为我们有这个接口的两个实现并且希望同时使用它们. |
||||
|
|
||||
|
在新的迁移项目(`Acme.BookStore.EntityFrameworkCore.DbMigrationsForSecondDb`)创建它的一个副本,内部使用 `BookStoreSecondMigrationsDbContext` . 示例实现: |
||||
|
|
||||
|
````csharp |
||||
|
public class EntityFrameworkCoreSecondBookStoreDbSchemaMigrator |
||||
|
: IBookStoreDbSchemaMigrator, ITransientDependency |
||||
|
{ |
||||
|
private readonly IServiceProvider _serviceProvider; |
||||
|
|
||||
|
public EntityFrameworkCoreSecondBookStoreDbSchemaMigrator( |
||||
|
IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
_serviceProvider = serviceProvider; |
||||
|
} |
||||
|
|
||||
|
public async Task MigrateAsync() |
||||
|
{ |
||||
|
/* We are intentionally resolving the BookStoreSecondMigrationsDbContext |
||||
|
* from IServiceProvider (instead of directly injecting it) |
||||
|
* to properly get the connection string of the current tenant in the |
||||
|
* current scope. |
||||
|
*/ |
||||
|
|
||||
|
await _serviceProvider |
||||
|
.GetRequiredService<BookStoreSecondMigrationsDbContext>() |
||||
|
.Database |
||||
|
.MigrateAsync(); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
> 类的名称对于[依赖注入](Dependency-Injection.md)非常重要. 为了可以使用 `IBookStoreDbSchemaMigrator` 引用注入它的结尾应该是 `BookStoreDbSchemaMigrator`. |
||||
|
|
||||
|
现在我们有 `IBookStoreDbSchemaMigrator` 接口的两个实现,每个实现负责迁移相关的数据库模式. |
||||
|
|
||||
|
#### 为第二个迁移项目定义一个模块类 |
||||
|
|
||||
|
是时候为第二次迁移(`Acme.BookStore.EntityFrameworkCore.DbMigrationsForSecondDb`)项目定义[模块](Module-Development-Basics.md)类了: |
||||
|
|
||||
|
````csharp |
||||
|
[DependsOn( |
||||
|
typeof(BookStoreEntityFrameworkCoreModule) |
||||
|
)] |
||||
|
public class BookStoreEntityFrameworkCoreSecondDbMigrationsModule : AbpModule |
||||
|
{ |
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
context.Services.AddAbpDbContext<BookStoreSecondMigrationsDbContext>(); |
||||
|
} |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
在 `Acme.BookStore.DbMigrator` 项目中引用 `Acme.BookStore.EntityFrameworkCore.DbMigrationsForSecondDb` 项目并且将 `typeof(BookStoreEntityFrameworkCoreSecondDbMigrationsModule)` 依赖项添加到 `BookStoreDbMigratorModule` 的依赖项列表中. `BookStoreDbMigratorModule` 类应该是这样的: |
||||
|
|
||||
|
````csharp |
||||
|
[DependsOn( |
||||
|
typeof(AbpAutofacModule), |
||||
|
typeof(BookStoreEntityFrameworkCoreDbMigrationsModule), |
||||
|
typeof(BookStoreEntityFrameworkCoreSecondDbMigrationsModule), // ADDED THIS! |
||||
|
typeof(BookStoreApplicationContractsModule) |
||||
|
)] |
||||
|
public class BookStoreDbMigratorModule : AbpModule |
||||
|
{ |
||||
|
... |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
#### BookStoreDbMigrationService |
||||
|
|
||||
|
最后还需要找到 `Acme.BookStore.Domain` 项目的 `BookStoreDbMigrationService` 类,它当前被设计为使用单个 `IBookStoreDbSchemaMigrator` 实现,但现在我们有了两个. |
||||
|
|
||||
|
它注入了 `IBookStoreDbSchemaMigrator`. 使用 `IEnumerable<IBookStoreDbSchemaMigrator>` 注入替换它, ([依赖注入系统](Dependency-Injection.md) 允许像这样注入接口的多个实现). |
||||
|
|
||||
|
现在你有 **模式迁移器的集合** ,找到以下行: |
||||
|
|
||||
|
````csharp |
||||
|
await _dbSchemaMigrators.MigrateAsync(); |
||||
|
```` |
||||
|
|
||||
|
将这行代码进行以下改变: |
||||
|
|
||||
|
````csharp |
||||
|
foreach (var migrator in _dbSchemaMigrators) |
||||
|
{ |
||||
|
await migrator.MigrateAsync(); |
||||
|
} |
||||
|
```` |
||||
|
|
||||
|
你可以运行 `.DbMigrator` 应用程序应用迁移和初始化种子数据. 你可以删除这两个数据库进行测试,然后再次运行 `.DbMigrator` 应用程序,它会创建两个数据库. |
||||
|
|
||||
|
## 结论 |
||||
|
|
||||
|
本文档说明了如何拆分数据库以及管理Entity Framework Core解决方案的数据库迁移. 简而言之,你需要为每个不同的数据库创建一个单独的迁移项目. |
||||
@ -0,0 +1,3 @@ |
|||||
|
# 身份管理模块 |
||||
|
|
||||
|
参阅 [源码](https://github.com/abpframework/abp/tree/dev/modules/identity). 文档很快会被完善. |
||||
@ -0,0 +1,3 @@ |
|||||
|
# 租户管理模块 |
||||
|
|
||||
|
TODO |
||||
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 3.9 KiB After Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 7.9 KiB After Width: | Height: | Size: 7.9 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 9.1 KiB After Width: | Height: | Size: 9.1 KiB |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 34 KiB |
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 32 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 2.9 KiB |
|
Before Width: | Height: | Size: 6.2 KiB After Width: | Height: | Size: 6.2 KiB |
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 34 KiB |
|
Before Width: | Height: | Size: 7.5 KiB After Width: | Height: | Size: 7.5 KiB |
|
Before Width: | Height: | Size: 7.9 KiB After Width: | Height: | Size: 7.9 KiB |
|
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 34 KiB |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
|
Before Width: | Height: | Size: 6.7 KiB After Width: | Height: | Size: 6.7 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 38 KiB |
|
After Width: | Height: | Size: 21 KiB |
|
After Width: | Height: | Size: 62 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 54 KiB |
@ -0,0 +1,19 @@ |
|||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.VirtualFileSystem; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpAspNetCoreMvcUiThemeSharedModule) |
||||
|
)] |
||||
|
public class AbpAspNetCoreMvcUiThemeSharedDemoModule : AbpModule |
||||
|
{ |
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
Configure<AbpVirtualFileSystemOptions>(options => |
||||
|
{ |
||||
|
options.FileSets.AddEmbedded<AbpAspNetCoreMvcUiThemeSharedDemoModule>("Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo"); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
||||
|
<ConfigureAwait /> |
||||
|
</Weavers> |
||||
@ -0,0 +1,30 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
||||
|
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. --> |
||||
|
<xs:element name="Weavers"> |
||||
|
<xs:complexType> |
||||
|
<xs:all> |
||||
|
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1"> |
||||
|
<xs:complexType> |
||||
|
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" /> |
||||
|
</xs:complexType> |
||||
|
</xs:element> |
||||
|
</xs:all> |
||||
|
<xs:attribute name="VerifyAssembly" type="xs:boolean"> |
||||
|
<xs:annotation> |
||||
|
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation> |
||||
|
</xs:annotation> |
||||
|
</xs:attribute> |
||||
|
<xs:attribute name="VerifyIgnoreCodes" type="xs:string"> |
||||
|
<xs:annotation> |
||||
|
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation> |
||||
|
</xs:annotation> |
||||
|
</xs:attribute> |
||||
|
<xs:attribute name="GenerateXsd" type="xs:boolean"> |
||||
|
<xs:annotation> |
||||
|
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation> |
||||
|
</xs:annotation> |
||||
|
</xs:attribute> |
||||
|
</xs:complexType> |
||||
|
</xs:element> |
||||
|
</xs:schema> |
||||
@ -0,0 +1,16 @@ |
|||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Widgets; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo.Views.Components.Themes.Shared.Demos.ButtonsDemo |
||||
|
{ |
||||
|
[Widget] |
||||
|
public class ButtonsDemoViewComponent : AbpViewComponent |
||||
|
{ |
||||
|
public const string ViewPath = "/Views/Components/Themes/Shared/Demos/ButtonsDemo/Default.cshtml"; |
||||
|
|
||||
|
public IViewComponentResult Invoke() |
||||
|
{ |
||||
|
return View(ViewPath); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
@using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo.Views.Components.Themes.Shared.Demos.ButtonsDemo |
||||
|
|
||||
|
<abp-component-demo-section title="Basics" view-path="@ButtonsDemoViewComponent.ViewPath"> |
||||
|
<abp-button text="Default" /> |
||||
|
<abp-button button-type="Primary" text="Primary" /> |
||||
|
<abp-button button-type="Secondary">Secondary</abp-button> |
||||
|
<abp-button button-type="Success">Success</abp-button> |
||||
|
<abp-button button-type="Danger">Danger</abp-button> |
||||
|
<abp-button button-type="Warning">Warning</abp-button> |
||||
|
<abp-button button-type="Info">Info</abp-button> |
||||
|
<abp-button button-type="Light">Light</abp-button> |
||||
|
<abp-button button-type="Dark">Dark</abp-button> |
||||
|
<abp-button button-type="Link">Link</abp-button> |
||||
|
</abp-component-demo-section> |
||||
|
|
||||
|
<abp-component-demo-section title="Outline" view-path="@ButtonsDemoViewComponent.ViewPath"> |
||||
|
<abp-button button-type="Outline_Primary">Primary</abp-button> |
||||
|
<abp-button button-type="Outline_Secondary">Secondary</abp-button> |
||||
|
<abp-button button-type="Outline_Success">Success</abp-button> |
||||
|
<abp-button button-type="Outline_Danger">Danger</abp-button> |
||||
|
<abp-button button-type="Outline_Warning">Warning</abp-button> |
||||
|
<abp-button button-type="Outline_Info">Info</abp-button> |
||||
|
<abp-button button-type="Outline_Light">Light</abp-button> |
||||
|
<abp-button button-type="Outline_Dark">Dark</abp-button> |
||||
|
</abp-component-demo-section> |
||||
|
|
||||
|
<abp-component-demo-section title="Icons" view-path="@ButtonsDemoViewComponent.ViewPath"> |
||||
|
<abp-button button-type="Warning" icon="pencil" text="Edit" /> |
||||
|
<abp-button button-type="Info" icon-type="FontAwesome" icon="info" text="Information" /> |
||||
|
</abp-component-demo-section> |
||||
@ -0,0 +1,105 @@ |
|||||
|
using System; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Razor.TagHelpers; |
||||
|
using Microsoft.Extensions.FileProviders; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers; |
||||
|
using Volo.Abp.VirtualFileSystem; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo.Views.Components.Themes.Shared.TagHelpers |
||||
|
{ |
||||
|
public class AbpComponentDemoSectionTagHelper : AbpTagHelper |
||||
|
{ |
||||
|
private const string DemoSectionOpeningTag = "<abp-component-demo-section"; |
||||
|
private const string DemoSectionClosingTag = "</abp-component-demo-section"; |
||||
|
|
||||
|
public string ViewPath { get; set; } |
||||
|
public string Title { get; set; } |
||||
|
|
||||
|
private readonly IVirtualFileProvider _virtualFileProvider; |
||||
|
|
||||
|
public AbpComponentDemoSectionTagHelper(IVirtualFileProvider virtualFileProvider) |
||||
|
{ |
||||
|
_virtualFileProvider = virtualFileProvider; |
||||
|
} |
||||
|
|
||||
|
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) |
||||
|
{ |
||||
|
output.TagName = null; |
||||
|
|
||||
|
var content = await output.GetChildContentAsync(); |
||||
|
|
||||
|
output.PreContent.AppendHtml("<div class=\"abp-component-demo-section\">"); |
||||
|
output.PreContent.AppendHtml($"<h2>{Title}</h2>"); |
||||
|
output.PreContent.AppendHtml("<div class=\"abp-component-demo-section-body\">"); |
||||
|
/* component rendering here */ |
||||
|
output.PostContent.AppendHtml("</div>"); //abp-component-demo-section-body
|
||||
|
AppendRawSource(output); |
||||
|
AppendBootstrapSource(output, content); |
||||
|
output.PostContent.AppendHtml("</div>"); //abp-component-demo-section
|
||||
|
} |
||||
|
|
||||
|
private static void AppendBootstrapSource(TagHelperOutput output, TagHelperContent content) |
||||
|
{ |
||||
|
output.PostContent.AppendHtml("<div class=\"abp-component-demo-section-bs-source\">"); |
||||
|
output.PostContent.AppendHtml("<h3>Bootstrap</h3>"); |
||||
|
output.PostContent.AppendHtml("<pre>"); |
||||
|
output.PostContent.Append(content.GetContent()); |
||||
|
output.PostContent.AppendHtml("</pre>"); |
||||
|
output.PostContent.AppendHtml("</div>"); |
||||
|
} |
||||
|
|
||||
|
private void AppendRawSource(TagHelperOutput output) |
||||
|
{ |
||||
|
output.PostContent.AppendHtml("<div class=\"abp-component-demo-section-raw-source\">"); |
||||
|
output.PostContent.AppendHtml("<h3>ABP Tag Helpers</h3>"); |
||||
|
output.PostContent.AppendHtml("<pre>"); |
||||
|
output.PostContent.Append(GetRawDemoSource()); |
||||
|
output.PostContent.AppendHtml("</pre>"); |
||||
|
output.PostContent.AppendHtml("</div>"); |
||||
|
} |
||||
|
|
||||
|
private string GetRawDemoSource() |
||||
|
{ |
||||
|
StringBuilder sourceBuilder = null; |
||||
|
|
||||
|
var lines = GetFileContent().SplitToLines(); |
||||
|
|
||||
|
foreach (var line in lines) |
||||
|
{ |
||||
|
if (line.Contains(DemoSectionOpeningTag) && GetName(line) == Title) |
||||
|
{ |
||||
|
sourceBuilder = new StringBuilder(); |
||||
|
} |
||||
|
else if (line.Contains(DemoSectionClosingTag, StringComparison.InvariantCultureIgnoreCase)) |
||||
|
{ |
||||
|
if (sourceBuilder == null) |
||||
|
{ |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
return sourceBuilder.ToString(); |
||||
|
} |
||||
|
else if (sourceBuilder != null) |
||||
|
{ |
||||
|
sourceBuilder.AppendLine(line); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
throw new AbpException($"Could not find {Title} demo section inside {ViewPath}"); |
||||
|
} |
||||
|
|
||||
|
private string GetFileContent() |
||||
|
{ |
||||
|
var viewFileInfo = _virtualFileProvider.GetFileInfo(ViewPath); |
||||
|
return viewFileInfo.ReadAsString(); |
||||
|
} |
||||
|
|
||||
|
private string GetName(string line) |
||||
|
{ |
||||
|
var str = line.Substring(line.IndexOf("title=\"", StringComparison.OrdinalIgnoreCase) + "title=\"".Length); |
||||
|
str = str.Left(str.IndexOf("\"", StringComparison.OrdinalIgnoreCase)); |
||||
|
return str; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,5 @@ |
|||||
|
@using System.Globalization |
||||
|
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
||||
|
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bootstrap |
||||
|
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bundling |
||||
|
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo |
||||
@ -0,0 +1,34 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk.Razor"> |
||||
|
|
||||
|
<Import Project="..\..\..\configureawait.props" /> |
||||
|
<Import Project="..\..\..\common.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp3.1</TargetFramework> |
||||
|
<AddRazorSupportForMvc>true</AddRazorSupportForMvc> |
||||
|
<AssemblyName>Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo</AssemblyName> |
||||
|
<PackageId>Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo</PackageId> |
||||
|
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<IsPackable>true</IsPackable> |
||||
|
<OutputType>Library</OutputType> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<EmbeddedResource Include="Views\**\*.cshtml" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<Content Remove="Views\**\*.cshtml" /> |
||||
|
<Content Remove="compilerconfig.json" /> |
||||
|
<Content Remove="Properties\launchSettings.json" /> |
||||
|
<None Include="Properties\launchSettings.json" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared\Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,25 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Volo.Abp.Cli.ProjectBuilding.Building |
||||
|
{ |
||||
|
public enum MobileApp |
||||
|
{ |
||||
|
None, |
||||
|
ReactNative |
||||
|
} |
||||
|
|
||||
|
public static class MobileAppExtensions{ |
||||
|
public static string GetFolderName(this MobileApp mobileApp) |
||||
|
{ |
||||
|
switch (mobileApp) |
||||
|
{ |
||||
|
case MobileApp.ReactNative: |
||||
|
return "react-native"; |
||||
|
default: |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
using System.IO; |
||||
|
using Microsoft.AspNetCore.Builder; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Hosting; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Bundling; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Bundling; |
||||
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo; |
||||
|
using Volo.Abp.Autofac; |
||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.UI; |
||||
|
using Volo.Abp.UI.Navigation; |
||||
|
using Volo.Abp.VirtualFileSystem; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpAspNetCoreMvcUiBasicThemeModule), |
||||
|
typeof(AbpAspNetCoreMvcUiThemeSharedDemoModule), |
||||
|
typeof(AbpAutofacModule) |
||||
|
)] |
||||
|
public class AbpAspNetCoreMvcUiThemeBasicDemoModule : AbpModule |
||||
|
{ |
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
var env = context.Services.GetHostingEnvironment(); |
||||
|
|
||||
|
if (env.IsDevelopment()) |
||||
|
{ |
||||
|
Configure<AbpVirtualFileSystemOptions>(options => |
||||
|
{ |
||||
|
options.FileSets.ReplaceEmbeddedByPhysical<AbpAspNetCoreMvcUiThemeSharedDemoModule>(Path.Combine(env.ContentRootPath, string.Format("..{0}..{0}src{0}Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo", Path.DirectorySeparatorChar))); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
Configure<AbpBundlingOptions>(options => |
||||
|
{ |
||||
|
options.StyleBundles |
||||
|
.Get(StandardBundles.Styles.Global) |
||||
|
.AddFiles("/demo/styles/main.css"); |
||||
|
}); |
||||
|
|
||||
|
Configure<AbpNavigationOptions>(options => |
||||
|
{ |
||||
|
options.MenuContributors.Add(new BasicThemeDemoMenuContributor()); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
||||
|
{ |
||||
|
var app = context.GetApplicationBuilder(); |
||||
|
var env = context.GetEnvironment(); |
||||
|
|
||||
|
if (env.IsDevelopment()) |
||||
|
{ |
||||
|
app.UseDeveloperExceptionPage(); |
||||
|
} |
||||
|
|
||||
|
app.UseVirtualFiles(); |
||||
|
app.UseRouting(); |
||||
|
app.UseMvcWithDefaultRouteAndArea(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.UI.Navigation; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo |
||||
|
{ |
||||
|
public class BasicThemeDemoMenuContributor : IMenuContributor |
||||
|
{ |
||||
|
public Task ConfigureMenuAsync(MenuConfigurationContext context) |
||||
|
{ |
||||
|
if(context.Menu.Name == StandardMenus.Main) |
||||
|
{ |
||||
|
AddMainMenuItems(context); |
||||
|
} |
||||
|
|
||||
|
return Task.CompletedTask; |
||||
|
} |
||||
|
|
||||
|
private void AddMainMenuItems(MenuConfigurationContext context) |
||||
|
{ |
||||
|
context.Menu.AddItem( |
||||
|
new ApplicationMenuItem("BasicThemeDemo.Components", "Components") |
||||
|
.AddItem( |
||||
|
new ApplicationMenuItem("BasicThemeDemo.Components.Buttons", "Buttons", url: "/Components/Buttons") |
||||
|
) |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,6 @@ |
|||||
|
@page |
||||
|
@using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo.Views.Components.Themes.Shared.Demos.ButtonsDemo |
||||
|
@model Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo.Pages.Components.Buttons.IndexModel |
||||
|
<h1>Buttons</h1> |
||||
|
|
||||
|
@await Component.InvokeAsync(typeof(ButtonsDemoViewComponent)) |
||||
@ -0,0 +1,12 @@ |
|||||
|
using Microsoft.AspNetCore.Mvc.RazorPages; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo.Pages.Components.Buttons |
||||
|
{ |
||||
|
public class IndexModel : PageModel |
||||
|
{ |
||||
|
public void OnGet() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
@page |
||||
|
@model Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo.Pages.IndexModel |
||||
|
<h1>Basic Theme Demo</h1> |
||||
@ -0,0 +1,12 @@ |
|||||
|
using Microsoft.AspNetCore.Mvc.RazorPages; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo.Pages |
||||
|
{ |
||||
|
public class IndexModel : PageModel |
||||
|
{ |
||||
|
public void OnGet() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
@using System.Globalization |
||||
|
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
||||
|
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bootstrap |
||||
|
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bundling |
||||
@ -0,0 +1,47 @@ |
|||||
|
using System; |
||||
|
using Microsoft.AspNetCore.Hosting; |
||||
|
using Microsoft.Extensions.Hosting; |
||||
|
using Serilog; |
||||
|
using Serilog.Events; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo |
||||
|
{ |
||||
|
public class Program |
||||
|
{ |
||||
|
public static int Main(string[] args) |
||||
|
{ |
||||
|
Log.Logger = new LoggerConfiguration() |
||||
|
.MinimumLevel.Debug() |
||||
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information) |
||||
|
.Enrich.FromLogContext() |
||||
|
.WriteTo.File("Logs/logs.txt") |
||||
|
.CreateLogger(); |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
Log.Information("Starting web host."); |
||||
|
CreateHostBuilder(args).Build().Run(); |
||||
|
return 0; |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
Log.Fatal(ex, "Host terminated unexpectedly!"); |
||||
|
return 1; |
||||
|
} |
||||
|
finally |
||||
|
{ |
||||
|
Log.CloseAndFlush(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
internal static IHostBuilder CreateHostBuilder(string[] args) => |
||||
|
Host.CreateDefaultBuilder(args) |
||||
|
.ConfigureWebHostDefaults(webBuilder => |
||||
|
{ |
||||
|
webBuilder.UseStartup<Startup>(); |
||||
|
}) |
||||
|
.UseAutofac() |
||||
|
.UseSerilog(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
{ |
||||
|
"iisSettings": { |
||||
|
"windowsAuthentication": false, |
||||
|
"anonymousAuthentication": true, |
||||
|
"iisExpress": { |
||||
|
"applicationUrl": "http://localhost:61659", |
||||
|
"sslPort": 0 |
||||
|
} |
||||
|
}, |
||||
|
"profiles": { |
||||
|
"IIS Express": { |
||||
|
"commandName": "IISExpress", |
||||
|
"launchBrowser": true, |
||||
|
"environmentVariables": { |
||||
|
"ASPNETCORE_ENVIRONMENT": "Development" |
||||
|
} |
||||
|
}, |
||||
|
"Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo": { |
||||
|
"commandName": "Project", |
||||
|
"launchBrowser": true, |
||||
|
"applicationUrl": "http://localhost:5000", |
||||
|
"environmentVariables": { |
||||
|
"ASPNETCORE_ENVIRONMENT": "Development" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
using Microsoft.AspNetCore.Builder; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Demo |
||||
|
{ |
||||
|
public class Startup |
||||
|
{ |
||||
|
public void ConfigureServices(IServiceCollection services) |
||||
|
{ |
||||
|
services.AddApplication<AbpAspNetCoreMvcUiThemeBasicDemoModule>(); |
||||
|
} |
||||
|
|
||||
|
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) |
||||
|
{ |
||||
|
app.InitializeApplication(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk.Web"> |
||||
|
|
||||
|
<Import Project="..\..\..\common.test.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp3.1</TargetFramework> |
||||
|
<PreserveCompilationReferences>true</PreserveCompilationReferences> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<Compile Remove="Logs\**" /> |
||||
|
<Content Remove="Logs\**" /> |
||||
|
<EmbeddedResource Remove="Logs\**" /> |
||||
|
<None Remove="Logs\**" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" /> |
||||
|
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" /> |
||||
|
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic\Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.csproj" /> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo\Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Demo.csproj" /> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,9 @@ |
|||||
|
{ |
||||
|
"Logging": { |
||||
|
"LogLevel": { |
||||
|
"Default": "Information", |
||||
|
"Microsoft": "Warning", |
||||
|
"Microsoft.Hosting.Lifetime": "Information" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
{ |
||||
|
"Logging": { |
||||
|
"LogLevel": { |
||||
|
"Default": "Information", |
||||
|
"Microsoft": "Warning", |
||||
|
"Microsoft.Hosting.Lifetime": "Information" |
||||
|
} |
||||
|
}, |
||||
|
"AllowedHosts": "*" |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
"use strict"; |
||||
|
|
||||
|
var gulp = require("gulp"), |
||||
|
path = require('path'), |
||||
|
copyResources = require('./node_modules/@abp/aspnetcore.mvc.ui/gulp/copy-resources.js'); |
||||
|
|
||||
|
exports.default = function(){ |
||||
|
return copyResources(path.resolve('./')); |
||||
|
}; |
||||
@ -0,0 +1,9 @@ |
|||||
|
{ |
||||
|
"version": "1.0.0", |
||||
|
"name": "asp.net", |
||||
|
"private": true, |
||||
|
"dependencies": { |
||||
|
"@abp/aspnetcore.mvc.ui.theme.basic": "^2.1.0" |
||||
|
}, |
||||
|
"devDependencies": {} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
.abp-component-demo-section { |
||||
|
border: 1px solid #999; |
||||
|
padding: 10px; |
||||
|
} |
||||
|
|
||||
|
.abp-component-demo-section-body { |
||||
|
padding-bottom: 10px; |
||||
|
} |
||||
|
|
||||
|
.abp-component-demo-section-raw-source { |
||||
|
background-color: #eee; |
||||
|
padding: 5px; |
||||
|
} |
||||
|
|
||||
|
.abp-component-demo-section-raw-source pre { |
||||
|
border: 1px solid #999; |
||||
|
margin: 5px; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
.abp-component-demo-section-bs-source { |
||||
|
background-color: #ddd; |
||||
|
padding: 5px; |
||||
|
} |
||||
|
|
||||
|
.abp-component-demo-section-bs-source pre { |
||||
|
border: 1px solid #999; |
||||
|
margin: 5px; |
||||
|
} |
||||
|
After Width: | Height: | Size: 699 KiB |
|
After Width: | Height: | Size: 141 KiB |
|
After Width: | Height: | Size: 829 KiB |