基于 abp vNext 和 .NET Core 开发博客项目 - 数据访问和代码优先

转载于:https://github.com/Meowv/Blog

本篇主要使用Entity Framework Core完成对数据库的访问,以及使用Code-First的方式进行数据迁移,自动创建表结构。

数据访问

在.EntityFrameworkCore项目中添加我们的数据访问上下文对象MeowvBlogDbContext,继承自 AbpDbContext。然后重写OnModelCreating方法。

OnModelCreating:定义EF Core 实体映射。先调用 base.OnModelCreating 让 abp 框架为我们实现基础映射,然后调用builder.Configure()扩展方法来配置应用程序的实体。当然也可以不用扩展,直接写在里面,这样一大坨显得不好看而已。

在abp框架中,可以使用 [ConnectionStringName] Attribute 为我们的DbContext配置连接字符串名称。先加上,然后再在appsettings.json中进行配置,因为之前集成了多个数据库,所以这里我们也配置多个连接字符串,与之对应。

本项目默认采用MySql,你可以选择任意你喜欢的。

//MeowvBlogDbContext.cs
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;

namespace Meowv.Blog.EntityFrameworkCore
{
[ConnectionStringName(“MySql”)]
public class MeowvBlogDbContext : AbpDbContext
{
public MeowvBlogDbContext(DbContextOptions options) : base(options)
{
}

    protected override void OnModelCreating(ModelBuilder modelBuilder){base.OnModelCreating(modelBuilder);modelBuilder.Configure();}
}

}

//appsettings.json
{
“ConnectionStrings”: {
“Enable”: “MySQL”,
“MySql”: “Server=localhost;User Id=root;Password=123456;Database=meowv_blog_tutorial”,
“SqlServer”: “”,
“PostgreSql”: “”,
“Sqlite”: “”
}
}

然后新建我们的扩展类MeowvBlogDbContextModelCreatingExtensions.cs和扩展方法Configure()。注意,扩展方法是静态的,需加static

//MeowvBlogDbContextModelCreatingExtensions.cs
using Microsoft.EntityFrameworkCore;
using Volo.Abp;

namespace Meowv.Blog.EntityFrameworkCore
{
public static class MeowvBlogDbContextModelCreatingExtensions
{
public static void Configure(this ModelBuilder builder)
{
Check.NotNull(builder, nameof(builder));

}
}
}

完成上述操作后在我们的模块类MeowvBlogFrameworkCoreModule中将DbContext注册到依赖注入,根据你配置的值使用不同的数据库。在.Domain层创建配置文件访问类AppSettings.cs

//AppSettings.cs
using Microsoft.Extensions.Configuration;
using System.IO;

namespace Meowv.Blog.Domain.Configurations
{
///
/// appsettings.json配置文件数据读取类
///
public class AppSettings
{
///
/// 配置文件的根节点
///
private static readonly IConfigurationRoot _config;

    /// <summary>/// Constructor/// </summary>static AppSettings(){// 加载appsettings.json,并构建IConfigurationRootvar builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", true, true);_config = builder.Build();}/// <summary>/// EnableDb/// </summary>public static string EnableDb => _config["ConnectionStrings:Enable"];/// <summary>/// ConnectionStrings/// </summary>public static string ConnectionStrings => _config.GetConnectionString(EnableDb);
}

}

获取配置文件内容比较容易,代码中有注释也很容易理解。

值得一提的是,ABP会自动为DbContext中的实体创建默认仓储. 需要在注册的时使用options添加AddDefaultRepositories()。

默认情况下为每个实体创建一个仓储,如果想要为其他实体也创建仓储,可以将 includeAllEntities 设置为 true,然后就可以在服务中注入和使用 IRepository 或 IQueryableRepository

//MeowvBlogFrameworkCoreModule.cs
using Meowv.Blog.Domain;
using Meowv.Blog.Domain.Configurations;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.MySQL;
using Volo.Abp.EntityFrameworkCore.PostgreSql;
using Volo.Abp.EntityFrameworkCore.Sqlite;
using Volo.Abp.EntityFrameworkCore.SqlServer;
using Volo.Abp.Modularity;

namespace Meowv.Blog.EntityFrameworkCore
{
[DependsOn(
typeof(MeowvBlogDomainModule),
typeof(AbpEntityFrameworkCoreModule),
typeof(AbpEntityFrameworkCoreMySQLModule),
typeof(AbpEntityFrameworkCoreSqlServerModule),
typeof(AbpEntityFrameworkCorePostgreSqlModule),
typeof(AbpEntityFrameworkCoreSqliteModule)
)]
public class MeowvBlogFrameworkCoreModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAbpDbContext(options =>
{
options.AddDefaultRepositories(includeAllEntities: true);
});

        Configure<AbpDbContextOptions>(options =>{switch (AppSettings.EnableDb){case "MySQL":options.UseMySQL();break;case "SqlServer":options.UseSqlServer();break;case "PostgreSql":options.UsePostgreSql();break;case "Sqlite":options.UseSqlite();break;default:options.UseMySQL();break;}});}
}

}

现在可以来初步设计博客所需表为:发表文章表(posts)、分类表(categories)、标签表(tags)、文章对应标签表(post_tags)、友情链接表(friendlinks)

在.Domain层编写实体类,Post.cs、Category.cs、Tag.cs、PostTag.cs、FriendLink.cs。把主键设置为int型,直接继承Entity。关于这点可以参考ABP文档,https://docs.abp.io/zh-Hans/abp/latest/Entities

//Post.cs
using System;
using Volo.Abp.Domain.Entities;

namespace Meowv.Blog.Domain.Blog
{
///
/// Post
///
public class Post : Entity
{
///
/// 标题
///
public string Title { get; set; }

    /// <summary>/// 作者/// </summary>public string Author { get; set; }/// <summary>/// 链接/// </summary>public string Url { get; set; }/// <summary>/// HTML/// </summary>public string Html { get; set; }/// <summary>/// Markdown/// </summary>public string Markdown { get; set; }/// <summary>/// 分类Id/// </summary>public int CategoryId { get; set; }/// <summary>/// 创建时间/// </summary>public DateTime CreationTime { get; set; }
}

}

//Category.cs
using Volo.Abp.Domain.Entities;

namespace Meowv.Blog.Domain.Blog
{
///
/// Category
///
public class Category : Entity
{
///
/// 分类名称
///
public string CategoryName { get; set; }

    /// <summary>/// 展示名称/// </summary>public string DisplayName { get; set; }
}

}

//Tag.cs
using Volo.Abp.Domain.Entities;

namespace Meowv.Blog.Domain.Blog
{
///
/// Tag
///
public class Tag : Entity
{
///
/// 标签名称
///
public string TagName { get; set; }

    /// <summary>/// 展示名称/// </summary>public string DisplayName { get; set; }
}

}

//PostTag.cs
using Volo.Abp.Domain.Entities;

namespace Meowv.Blog.Domain.Blog
{
///
/// PostTag
///
public class PostTag : Entity
{
///
/// 文章Id
///
public int PostId { get; set; }

    /// <summary>/// 标签Id/// </summary>public int TagId { get; set; }
}

}

//FriendLink.cs
using Volo.Abp.Domain.Entities;

namespace Meowv.Blog.Domain.Blog
{
///
/// FriendLink
///
public class FriendLink : Entity
{
///
/// 标题
///
public string Title { get; set; }

    /// <summary>/// 链接/// </summary>public string LinkUrl { get; set; }
}

}

创建好实体类后,在MeowvBlogDbContext添加DbSet属性

//MeowvBlogDbContext.cs

[ConnectionStringName(“MySql”)]
public class MeowvBlogDbContext : AbpDbContext
{
public DbSet Posts { get; set; }

    public DbSet<Category> Categories { get; set; }public DbSet<Tag> Tags { get; set; }public DbSet<PostTag> PostTags { get; set; }public DbSet<FriendLink> FriendLinks { get; set; }...
}

在.Domain.Shared层添加全局常量类MeowvBlogConsts.cs和表名常量类MeowvBlogDbConsts.cs,搞一个表前缀的常量,我这里写的是meowv_,大家可以随意。代表我们的表名都将以meowv_开头。然后在MeowvBlogDbConsts中将表名称定义好。

//MeowvBlogConsts.cs
namespace Meowv.Blog.Domain.Shared
{
///
/// 全局常量
///
public class MeowvBlogConsts
{
///
/// 数据库表前缀
///
public const string DbTablePrefix = “meowv_”;
}
}

//MeowvBlogDbConsts.cs
namespace Meowv.Blog.Domain.Shared
{
public class MeowvBlogDbConsts
{
public static class DbTableName
{
public const string Posts = “Posts”;

        public const string Categories = "Categories";public const string Tags = "Tags";public const string PostTags = "Post_Tags";public const string Friendlinks = "Friendlinks";}
}

}

在Configure()方法中配置表模型,包括表名、字段类型和长度等信息。对于下面代码不是很明白的可以看看微软的自定义 Code First 约定:https://docs.microsoft.com/zh-cn/ef/ef6/modeling/code-first/conventions/custom

//MeowvBlogDbContextModelCreatingExtensions.cs
using Meowv.Blog.Domain.Blog;
using Meowv.Blog.Domain.Shared;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using static Meowv.Blog.Domain.Shared.MeowvBlogDbConsts;

namespace Meowv.Blog.EntityFrameworkCore
{
public static class MeowvBlogDbContextModelCreatingExtensions
{
public static void Configure(this ModelBuilder builder)
{
Check.NotNull(builder, nameof(builder));

        builder.Entity<Post>(b =>{b.ToTable(MeowvBlogConsts.DbTablePrefix + DbTableName.Posts);b.HasKey(x => x.Id);b.Property(x => x.Title).HasMaxLength(200).IsRequired();b.Property(x => x.Author).HasMaxLength(10);b.Property(x => x.Url).HasMaxLength(100).IsRequired();b.Property(x => x.Html).HasColumnType("longtext").IsRequired();b.Property(x => x.Markdown).HasColumnType("longtext").IsRequired();b.Property(x => x.CategoryId).HasColumnType("int");b.Property(x => x.CreationTime).HasColumnType("datetime");});builder.Entity<Category>(b =>{b.ToTable(MeowvBlogConsts.DbTablePrefix + DbTableName.Categories);b.HasKey(x => x.Id);b.Property(x => x.CategoryName).HasMaxLength(50).IsRequired();b.Property(x => x.DisplayName).HasMaxLength(50).IsRequired();});builder.Entity<Tag>(b =>{b.ToTable(MeowvBlogConsts.DbTablePrefix + DbTableName.Tags);b.HasKey(x => x.Id);b.Property(x => x.TagName).HasMaxLength(50).IsRequired();b.Property(x => x.DisplayName).HasMaxLength(50).IsRequired();});builder.Entity<PostTag>(b =>{b.ToTable(MeowvBlogConsts.DbTablePrefix + DbTableName.PostTags);b.HasKey(x => x.Id);b.Property(x => x.PostId).HasColumnType("int").IsRequired();b.Property(x => x.TagId).HasColumnType("int").IsRequired();});builder.Entity<FriendLink>(b =>{b.ToTable(MeowvBlogConsts.DbTablePrefix + DbTableName.Friendlinks);b.HasKey(x => x.Id);b.Property(x => x.Title).HasMaxLength(20).IsRequired();b.Property(x => x.LinkUrl).HasMaxLength(100).IsRequired();});}
}

}

此时项目层级目录如下

图片

代码优先

在.EntityFrameworkCore.DbMigrations中新建模块类MeowvBlogEntityFrameworkCoreDbMigrationsModule.cs、数据迁移上下文访问对象MeowvBlogMigrationsDbContext.cs和一个Design Time Db Factory类MeowvBlogMigrationsDbContextFactory.cs

模块类依赖MeowvBlogFrameworkCoreModule模块和AbpModule。并在ConfigureServices方法中添加上下文的依赖注入。

//MeowvBlogEntityFrameworkCoreDbMigrationsModule.cs
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;

namespace Meowv.Blog.EntityFrameworkCore.DbMigrations.EntityFrameworkCore
{
[DependsOn(
typeof(MeowvBlogFrameworkCoreModule)
)]
public class MeowvBlogEntityFrameworkCoreDbMigrationsModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAbpDbContext();
}
}
}

MeowvBlogMigrationsDbContext和MeowvBlogDbContext没什么大的区别

//MeowvBlogMigrationsDbContext.cs
using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace Meowv.Blog.EntityFrameworkCore.DbMigrations.EntityFrameworkCore
{
public class MeowvBlogMigrationsDbContext : AbpDbContext
{
public MeowvBlogMigrationsDbContext(DbContextOptions options) : base(options)
{

    }protected override void OnModelCreating(ModelBuilder builder){base.OnModelCreating(builder);builder.Configure();}
}

}
MeowvBlogMigrationsDbContextFactory类主要是用来使用Code-First命令的(Add-Migration 和 Update-Database …)

需要注意的地方,我们在这里要单独设置配置文件的连接字符串,将.HttpApi.Hosting层的appsettings.json复制一份到.EntityFrameworkCore.DbMigrations,你用了什么数据库就配置什么数据库的连接字符串。

//appsettings.json
{
“ConnectionStrings”: {
“Default”: “Server=localhost;User Id=root;Password=123456;Database=meowv_blog”
}
}

//MeowvBlogMigrationsDbContextFactory.cs
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace Meowv.Blog.EntityFrameworkCore.DbMigrations.EntityFrameworkCore
{
public class MeowvBlogMigrationsDbContextFactory : IDesignTimeDbContextFactory
{
public MeowvBlogMigrationsDbContext CreateDbContext(string[] args)
{
var configuration = BuildConfiguration();

        var builder = new DbContextOptionsBuilder<MeowvBlogMigrationsDbContext>().UseMySql(configuration.GetConnectionString("Default"));return new MeowvBlogMigrationsDbContext(builder.Options);}private static IConfigurationRoot BuildConfiguration(){var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);return builder.Build();}
}

}
到这里差不多就结束了,默认数据库meowv_blog_tutorial是不存在的,先去创建一个空的数据库。

图片

然后在Visual Studio中打开程序包管理控制台,将.EntityFrameworkCore.DbMigrations设为启动项目。

图片

键入命令:Add-Migration Initial,会发现报错啦,错误信息如下:

Add-Migration : 无法将“Add-Migration”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 1

  • Add-Migration Initial
  •   + CategoryInfo          : ObjectNotFound: (Add-Migration:String) [], CommandNotFoundException+ FullyQualifiedErrorId : CommandNotFoundException

这是因为我们少添加了一个包,要使用代码优先方式迁移数据,必须添加,Microsoft.EntityFrameworkCore.Tools。

紧接着直接用命令安装Install-Package Microsoft.EntityFrameworkCore.Tools包,再试一遍

图片

可以看到已经成功,并且生成了一个Migrations文件夹和对应的数据迁移文件。

最后输入更新命令:Update-Database,然后打开数据瞅瞅。

图片

完美,成功创建了数据库表,而且命名也是我们想要的,字段类型也是ok的。__efmigrationshistory表是用来记录迁移历史的,这个可以不用管。当我们后续如果想要修改添加表字段,新增表的时候,都可以使用这种方式来完成。

解决方案层级目录图,供参考

图片

本篇使用Entity Framework Core完成数据访问和代码优先的方式创建数据库表,你学会了吗?

基于 abp vNext 和 .NET Core 开发博客项目 - 数据访问和代码优先相关推荐

  1. 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(一)

    系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...

  2. 基于 abp vNext 和 .NET Core 开发博客项目 - 终结篇之发布项目

    基于 abp vNext 和 .NET Core 开发博客项目 - 终结篇之发布项目 转载于:https://github.com/Meowv/Blog 既然开发完成了,还是拿出来溜溜比较好,本篇是本 ...

  3. 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(九)

    基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(九) 转载于:https://github.com/Meowv/Blog 终于要接近尾声了,上一篇基本上将文 ...

  4. 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(八)

    基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(八) 转载于:https://github.com/Meowv/Blog 上一篇完成了标签模块和友情链接模块 ...

  5. 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(七)

    基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(七) 转载于:https://github.com/Meowv/Blog 上一篇完成了后台分类模块的所有功能 ...

  6. 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(六)

    基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(六) 转载于:https://github.com/Meowv/Blog 上一篇完成了博客文章详情页面的数据 ...

  7. 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(五)

    基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(五) 转载于:https://github.com/Meowv/Blog 上一篇完成了分类标签友链的列表查询 ...

  8. 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(四)

    基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(四) 转载于:https://github.com/Meowv/Blog 上一篇完成了博客的分页查询文章列表 ...

  9. 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(三)

    基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(三) 转载于:https://github.com/Meowv/Blog 上一篇完成了博客的主题切换,菜单和 ...

最新文章

  1. 【控制】传递函数拉氏变化如何与时间域结合使用举例
  2. Tyznn人脸识别温度检测智能门禁系统现货发售,助力疫情防控
  3. 学计算机平面设计可以找什么工作,大学生学了平面设计之后能找什么样的工作...
  4. xp系统 javafx_使用JavaFX构建React系统
  5. 华为畅享8plus停产了吗_华为畅享8Plus
  6. UI设计干货模板|引导网格系统
  7. pythonqt5plaintextedit某一行的内容_如何能够做到持续输出内容?
  8. 帝豪gl车机系统降级_何以剑指合资?帝豪GL/英朗底盘对比
  9. BZOJ2217 [Poi2011]Lollipop 【贪心】
  10. dubbo架构概览-dubbo源码解析
  11. 露天停车场的matlab代码,室外停车场设计规范 · 干货
  12. MIMO技术(一)分集与复用
  13. vs code git 编辑器中拉取(pull) 的时候报错 [rejected] v1.0.0 -> v1.0.0 (would clobber existing tag)
  14. 如何解决微信与此ipad不兼容
  15. ★中国富豪掘第一桶金的九大方式 ★
  16. 《编程之美》学习笔记
  17. 本程序实现求n*m的二维数组元素的最大值,请将程序补充完整,以实现规定功能
  18. amp;amp;什么意思?
  19. QQ虎年春节活动ADB自动助手(自动开星星,自动红包雨下拉,自动团圆饭,自动一笔连)
  20. 31套VTK3D图像体绘制/VTK光线投射法/VTK三维重建程序源码

热门文章

  1. 大道至简,SQL也可以实现神经网络
  2. 技术系列课|“主动降噪”到底有多厉害?
  3. 操作系统期末复习知识点
  4. 个人开发者接入支付宝,Android开发接入支付宝支付...
  5. 【Android Studio安装部署系列】十八、Android studio更换APP应用图标
  6. 不等双十一,ChemDraw降价活动已经打开!
  7. 如何让springboot中的某些html文件不经过thymeleaf模板解析?
  8. ElasticSearch搜索实例含高亮显示及搜索的特殊字符过滤
  9. PostgreSQL csvlog 源码分析
  10. iptables与tomcat