在.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 { /// <summary> /// Post /// </summary> public class Post : Entity<int> { /// <summary> /// 标题 /// </summary> 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 { /// <summary> /// Category /// </summary> public class Category : Entity<int> { /// <summary> /// 分类名称 /// </summary> public string CategoryName { get; set; } /// <summary> /// 展示名称 /// </summary> public string DisplayName { get; set; } } } //Tag.cs using Volo.Abp.Domain.Entities; namespace Meowv.Blog.Domain.Blog { /// <summary> /// Tag /// </summary> public class Tag : Entity<int> { /// <summary> /// 标签名称 /// </summary> public string TagName { get; set; } /// <summary> /// 展示名称 /// </summary> public string DisplayName { get; set; } } } //PostTag.cs using Volo.Abp.Domain.Entities; namespace Meowv.Blog.Domain.Blog { /// <summary> /// PostTag /// </summary> public class PostTag : Entity<int> { /// <summary> /// 文章Id /// </summary> 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 { /// <summary> /// FriendLink /// </summary> public class FriendLink : Entity<int> { /// <summary> /// 标题 /// </summary> public string Title { get; set; } /// <summary> /// 链接 /// </summary> public string LinkUrl { get; set; } } }创建好实体类后,在MeowvBlogDbContext添加DbSet属性
//MeowvBlogDbContext.cs ... [ConnectionStringName("MySql")] public class MeowvBlogDbContext : AbpDbContext<MeowvBlogDbContext> { public DbSet<Post> 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 { /// <summary> /// 全局常量 /// </summary> public class MeowvBlogConsts { /// <summary> /// 数据库表前缀 /// </summary> 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(); }); } } }此时项目层级目录如下