通过对.net core的简单尝试,我发现以往我们开发mvc项目时,是通过新建一个.edmx文件来生成和更新实体模型,但是在core中,微软去掉了.edmx,所以下面我就来说一下core中如何生成model类。
环境:vs2017 + sqlserver2012
第一步 我们先创建测试库 create database [blogging];gouse [blogging];gocreate table [blog] ( [blogid] int not null identity, [url] nvarchar(max) not null, constraint [pk_blog] primary key ([blogid]) );gocreate table [post] ( [postid] int not null identity, [blogid] int not null, [content] nvarchar(max), [title] nvarchar(max), constraint [pk_post] primary key ([postid]), constraint [fk_post_blog_blogid] foreign key ([blogid]) references [blog] ([blogid]) on delete cascade);goinsert into [blog] (url) values('http://blogs.msdn.com/dotnet'), ('http://blogs.msdn.com/webdev'), ('http://blogs.msdn.com/visualstudio')go
第二步 创建一个.net core项目 略
第三步 安装ef nuget package manager
通过nuget安装:
第四步 通过数据库创建实体模型 tools – nuget package manager – a model the existing . you receive an error stating the term recognized the name a cmdlet,
项目会生成一个model文件夹,里面有我们需要的实体类和上下文bloggingcontext.cs
完成!因为我们只介绍如何生成实体类,所以就到此为止,如果想操作实体类增删改查,我们还需要注册上下文在startup.cs文件里,具体可以参考微软的说明文档:
https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db
以上就是.net core 根据数据库生成实体类的详细内容。