天天看點

使用控制台從0開始嘗試Abp.EntityFramwork

引言

使用空白控制台複寫一下

Abp.vNEXT

中對

EF

的使用。

所依賴的包

  • Volo.Abp.Data
  • Volo.Abp.EntityFrameworkCore
  • Volo.Abp.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools (為了使用dotnet ef/add-miration)
我這裡預設選擇SqlServer,其實Abp是支援很多種資料庫的。

1. 添加Module

using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Modularity;

namespace csblog_133043
{
    public class CnBlogModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpDbContextOptions>(options => options.UseSqlServer());
        }
    }
}
           

2. 添加實體

using System;
using System.Collections.Generic;
using System.Text;

namespace csblog_133043
{
    public class Users
    {
        public int Id { get; set; }

        public Users Referrer { get; set; }
    }
    public class Article
    {
        public int Id { get; set; }
    
        public Article Next { get; set; }

        public Article Previous { get; set; }
    }
}
           
我這裡隻是簡單的使用,其實Abp中還是有很多的審計聚合根,在領域設計中直接開放set也不是最佳實踐。

3. 添加DbContextFactory

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace csblog_133043
{
    public class CnBlog133043MigrationsContextFactory : IDesignTimeDbContextFactory<CnBlog133043Context>
    {
        public CnBlog133043Context CreateDbContext(string[] args)
        {
            var configuration = BuildConfiguration();

            var builder = new DbContextOptionsBuilder<CnBlog133043Context>()
                .UseSqlServer(configuration.GetConnectionString("Default"));

            return new CnBlog133043Context(builder.Options);
        }
        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false);

            return builder.Build();
        }
    }
}
           
ContextFactory是為了使用dotnet ef 或add-migration時傳回對應的DbContext。

4. 建立DbContext

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace csblog_133043
{
    [ConnectionStringName("Default")]
    public class CnBlog133043Context : AbpDbContext<CnBlog133043Context>
    {
        public CnBlog133043Context(DbContextOptions<CnBlog133043Context> options)
            : base(options)
        {
        }

        public DbSet<Article> Articles { get; set; }

        public DbSet<Users> Users { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Users>(u=>
            {
                u.ToTable(nameof(Users));

                u.ConfigureByConvention();

                u.HasOne(n => n.Referrer).WithOne();
            });

            modelBuilder.Entity<Article>(a=>
            {
                a.ToTable(nameof(Article));

                a.ConfigureByConvention();

                a.HasOne(n => n.Next).WithOne();
                a.HasOne(n => n.Previous).WithOne();
            });
        }
           

5. 添加appsettings

{
  "ConnectionStrings": {
    "Default": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=cnblog;Integrated Security=True"
  }
}
           
.net core架構預設都會找ConnectionStrings

6. 開始Migration

使用控制台從0開始嘗試Abp.EntityFramwork

不清楚的可以參見我前面介紹的關于EF篇:

  • EFCore中代碼優先锲約和資料類型與資料庫的對應關系
  • efcore 一對一 一對多 多對多關系

7. 測試的效果:

使用控制台從0開始嘗試Abp.EntityFramwork

8. 寫在最後

本篇主要是對

Abp.vNext

中如何使用

EntityFramwork

做了一個從0搭建項目做一個簡單的demo記錄,隻是使用到了一對一的展示,其他的就不再累述。

再就是

Abp.vNext

中還有很多的審計聚合根的使用,擴充屬性等等,在設計Entity上也是DDD的思想,需要慢慢體會裡面的思想以及韻味。