一、ASP.NET Zero 多表關聯
ASP.NET Zero是一個基於ASP.NET Core的跨平台Web應用程序框架。在ASP.NET Zero中,我們可以非常方便地完成多表之間的關聯操作。在實現多表之間的關聯時,我們需要使用Entity Framework Core來設置各種實體之間的關係。
public class Order : FullAuditedAggregateRoot { [ForeignKey("Customer")] public long CustomerId { get; set; } public virtual Customer Customer { get; set; } [ForeignKey("Salesperson")] public long SalespersonId { get; set; } public virtual Salesperson Salesperson { get; set; } } public class Customer : FullAuditedAggregateRoot { public string Name { get; set; } public virtual ICollection Orders { get; set; } } public class Salesperson : FullAuditedAggregateRoot { public string Name { get; set; } public virtual ICollection Orders { get; set; } }
在上述代碼示例中,我們定義了三個實體,分別為Order、Customer和Salesperson。其中Order實體擁有一個外鍵關聯到Customer實體和Salesperson實體。在Customer和Salesperson實體中,分別聲明了與Order實體的導航屬性。這樣就完成了多表之間的關聯配置。
二、ASP.NET Zero 10.4.0
ASP.NET Zero的10.4.0版本在原來的基礎上增加了一些新的特性。其中比較重要的特性包括跨域請求、實現多語言支持以及實現分散式事務。
跨域請求是指在一個域名下的頁面向另一個域名的頁面發送請求的行為。在ASP.NET Zero中,我們可以使用CORS(跨域資源共享)來實現跨域請求。實現CORS的方式比較簡單,只需在startup.cs文件中的ConfigureServices方法中添加以下代碼:
services.AddCors(options => { options.AddPolicy("AllowAll", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); }); app.UseCors("AllowAll");
實現多語言支持是指在應用程序中可以動態更改用戶的語言偏好設置。在ASP.NET Zero中,我們可以使用AspNetZeroLocalizationConfigurer來實現多語言支持。具體實現方式如下:
public override void ConfigureLocalizationService(ILocalizationServiceConfiguration localizationServiceConfiguration) { localizationServiceConfiguration.DefaultResourceType = typeof(MyAppResource); localizationServiceConfiguration.Languages.Add(new LanguageInfo("en", "English", "famfamfam-flag-gb", isDefault: true)); localizationServiceConfiguration.Languages.Add(new LanguageInfo("zh-Hans", "簡體中文", "famfamfam-flag-cn")); }
實現分散式事務是指在分散式環境下多個資料庫之間進行數據交互時,可以保證數據的一致性。在ASP.NET Zero中,我們可以使用ABP框架自帶的UnitOfWork來實現分散式事務。在代碼中使用工作單元,如果拋出異常,則會自動回滾事務。
public class OrderAppService : MyProjectAppServiceBase, IOrderAppService { private readonly IRepository _orderRepository; private readonly IRepository _customerRepository; private readonly IRepository _salespersonRepository; public OrderAppService( IRepository orderRepository, IRepository customerRepository, IRepository salespersonRepository) { _orderRepository = orderRepository; _customerRepository = customerRepository; _salespersonRepository = salespersonRepository; } public async Task CreateOrder(CreateOrderInput input) { using (var uow = UnitOfWorkManager.Begin()) { try { // 創建訂單 var order = new Order { CustomerId = input.CustomerId, SalespersonId = input.SalespersonId, CreationTime = Clock.Now }; await _orderRepository.InsertAsync(order); // 修改客戶信息 var customer = await _customerRepository.GetAsync(input.CustomerId); customer.Name = input.CustomerName; await _customerRepository.UpdateAsync(customer); // 修改銷售員信息 var salesperson = await _salespersonRepository.GetAsync(input.SalespersonId); salesperson.Name = input.SalespersonName; await _salespersonRepository.UpdateAsync(salesperson); await uow.CompleteAsync(); } catch (Exception) { await uow.RollbackAsync(); throw; } } } }
三、ASP.NET Zero連接兩個資料庫
ASP.NET Zero可以輕鬆地連接多個資料庫。在連接多個資料庫時,我們可以配置不同的DbContext。
我們可以使用ASP.NET Zero提供的ABP框架自帶的DbContext來實現和使用不同的資料庫。在配置多個DbContext時,需要在startup.cs文件中的ConfigureServices方法中添加以下代碼:
services.AddAbpDbContext(options => { options.DbContextOptions.UseSqlServer(connectionStrings.Default); }); services.AddAbpDbContext(options => { options.DbContextOptions.UseSqlServer(connectionStrings.Order); });
具體代碼實現示例
public class OrderDbContext : AbpDbContext { public OrderDbContext(DbContextOptions options) : base(options) { } public DbSet Orders { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.ConfigureOrderStore(); } } public static class OrderDbContextModelCreatingExtensions { public static void ConfigureOrderStore(this ModelBuilder builder) { builder.Entity(b => { b.ToTable("Orders"); b.ConfigureByConvention(); }); } }
在上述代碼示例中,我們自定義了一個名為OrderDbContext的DbContext,並在其中定義了一個DbSet。我們還在ConfigureServices中將DbContext的配置細節添加了進去。
我們還可以在代碼中使用連接另一個資料庫來查詢數據:
public async Task<IList> GetOrdersFromOtherDbContext(long customerId) { using (var orderDbContext = _serviceProvider.GetService()) { return await orderDbContext.Orders.FromSqlRaw("SELECT * FROM Orders WHERE CustomerId = {0}", customerId).ToListAsync(); } }
在上述代碼示例中,我們在代碼中使用了OrderDbContext來連接另一個資料庫,並使用FromSqlRaw來執行查詢,並得到了查詢結果。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/193528.html