asp.net
基于 abp vnext 和 .net core 开发博客项目 -凯发ag旗舰厅登录网址下载
基于 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)
{
}
}
//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;
}
获取配置文件内容比较容易,代码中有注释也很容易理解。
值得一提的是,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);
});
}
现在可以来初步设计博客所需表为:发表文章表(posts)、分类表(categories)、标签表(tags)、文章对应标签表(post_tags)、凯发ag旗舰厅登录网址下载的友情链接表(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; }
}
//category.cs
using volo.abp.domain.entities;
namespace meowv.blog.domain.blog
{
///
/// category
///
public class category : entity
{
///
/// 分类名称
///
public string categoryname { get; set; }
}
//tag.cs
using volo.abp.domain.entities;
namespace meowv.blog.domain.blog
{
///
/// tag
///
public class tag : entity
{
///
/// 标签名称
///
public string tagname { 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; }
}
//friendlink.cs
using volo.abp.domain.entities;
namespace meowv.blog.domain.blog
{
///
/// friendlink
///
public class friendlink : entity
{
///
/// 标题
///
public string title { get; set; }
}
创建好实体类后,在meowvblogdbcontext添加dbset属性
//meowvblogdbcontext.cs
…
[connectionstringname(“mysql”)]
public class meowvblogdbcontext : abpdbcontext
{
public dbset posts { 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”;
}
在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));
}
此时项目层级目录如下
图片
代码优先
在.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)
{
}
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();
}
到这里差不多就结束了,默认数据库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表是用来记录迁移历史的,这个可以不用管。当我们后续如果想要修改添加表字段,新增表的时候,都可以使用这种方式来完成。
凯发ag旗舰厅登录网址下载的解决方案层级目录图,供参考
图片
本篇使用entity framework core完成数据访问和代码优先的方式创建数据库表,你学会了吗?😁😁😁
开源地址:https://github.com/meowv/blog/tree/blog_tutorial
与50位技术专家面对面20年技术见证,附赠技术全景图总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: 基于 abp vnext 和 .net