纳税评估工作总结,福利所第一导航福利,反舰导弹
在 .net core 2.2中 microsoft.aspnetcore.app 默认内置了entityframework core 包,所以在使用过程中,我们无需再从 nuget 仓库单独应用 efcore 包;本文并不打算深入的介绍 efcore 的各种使用方式、原理解析,本文重点在于解决让初学者在10分钟内快速使用上 efcore 的问题。
efcore 支持 code first 方式,这个特性允许开发人员基于业务实体模型创建数据库
public class topic { public int id { get; set; } public string title { get; set; } public string content { get; set; } public datetime createtime { get; set; } public virtual icollection<post> posts { get; set; } } public class post { public int id { get; set; } public int topicid { get; set; } public string content { get; set; } public datetime createtime { get; set; } public virtual topic topic { get; set; } }
上面定义的两个实体对象之间通过 topic.posts 和 post.topic 属性建立了主外键关系,这两个表的主键为 id,且类型为 int ,这表示在下面的创建数据库过程中,efcore 会自动的为这两个实体对象建立关系和主键,并会自动设置 id 字段为主键标识
dbcontext 内置了很多个构造函数,这里使用配置选项的方式,实现方式也非常简单,最终,在 forumcontext 类中定义上面的实体业务模型集合即可
public class forumcontext : dbcontext { public forumcontext(dbcontextoptions<forumcontext> options) : base(options) { } public dbset<topic> topics { get; set; } public dbset<post> posts { get; set; } }
"connectionstrings": { "forum": "server=.\\sqlexpress;uid=sa;pwd=123456;database=forum;" }
public void configureservices(iservicecollection services) { services.adddbcontext<forumcontext>(options => { var connectionstring = this.configuration["connectionstrings:forum"]; options.usesqlserver(connectionstring); }); ... }
上面的代码使用 adddbcontext
方法,并配置了数据库连接字符串为配置文件中的 "connectionstrings:forum" 节点的值
现在,打开项目中的包管理器控制台
add-migration forum_v1 update-database
在输入命令 add-migration forum_v1 后,回车,控制台输出 to undo this action, use remove-migration. 表示命令执行成功;同时可以看到,在项目中多了一个文件夹 migrations;
注意:此时,数据库 forum 并没有被创建
在 migrations 文件夹中;当执行 update-database 命令后,efcore 设计工具将根据 migrations 中的定义去创建数据库,最终,控制台输出 done 表示创建完成
从上图中可以看到,数据库创建成功,同时,forum 数据库中还多了一个表 __efmigrationshistory ,该表存储的正是我们项目中的 migrations 的内容,只有两个字段,对应 20190109031435_forum_v1.cs 和 当前使用的 efcore 版本号
如果后续有增加实体,只需要再次执行 add-migration 命令即可
如果希望获得帮助,还可在包管理器控制台执行命令 get-help add-migration
至此,数据库创建完成,为了在控制器中使用 forumcontext 对象,我们在 homecontroller 中使用依赖注入的方式获得 formcontext 对象,以备后续使用
private forumcontext context; public homecontroller(forumcontext context) { this.context = context; }
现在,尝试着在项目中执行一些增删改查的工作,插入一条 topic 记录,在 homecontroller 中编写以下代码
[route("api/[controller]"), apicontroller] public class homecontroller : controllerbase { private forumcontext context; public homecontroller(forumcontext context) { this.context = context; } [httpget] public actionresult<ienumerable<topic>> get() { var topics = context.topics.tolist(); return topics; } [httppost] public void post([frombody] topicviewmodel model) { context.topics.add(new topic() { content = model.content, createtime = datetime.now, title = model.title }); context.savechanges(); } [httpput] public void put([frombody] topicviewmodel model) { var topic = context.topics.where(f => f.id == model.id).firstordefault(); topic.title = model.title; topic.content = model.content; context.topics.update(topic); context.savechanges(); } [httpdelete("{id}")] public void delete(int id) { var topic = context.topics.where(f => f.id == id).firstordefault(); context.topics.remove(topic); context.savechanges(); } }
上面的代码定义了 get/post/put/delete 接口,这是一个标准的 resetful api ,通过依次调用模拟对数据库的 curd 操作
在很多时候,我们的开发方式是先设计好数据库模型,然后再生成实体对象,这种方式对于从其它语言迁移到 .net core 上非常友好,从现有数据库中生成实体对象非常简单,只需要一个命令即可,还是以上面创建好的数据库 forum 为例子
`scaffold-dbcontext "server=.\sqlexpress;uid=sa;pwd=123456;database=forum" microsoft.entityframeworkcore.sqlserver -outputdir dbmodels
如果仅需要生成部分数据表,还可以通过将 -tables 参数添加到上述命令来指定要为哪些表生成实体。 例如 -tables blog,post。多个数据表以逗号分隔
通过查看生成的代码比较,和 code first 方式基本相同,使用方式完全一致
不管是 code first 还是 db first ,在实体对象中,我们都可以看到有个一个导航属性,比如 topic.posts 和 post.topic ,该导航属性定义了前缀 virtual 表示延迟加载此关联对象,在 code first 中,导航属性还起到主外键关系定义的作用
https://github.com/lianggx/easyaspnetcoredemo/tree/master/ron.mssql
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
asp.net搭建博客,使用BlogEngine.NET+MySql搭建博客
网友评论