您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

.net core 根据数据库生成实体类

2024/12/26 4:13:34发布15次查看
微软最近几年在跨平台上不断发力,很多.net程序员也摩拳擦掌,对微软寄以厚望。就在最近,微软还推出了asp .net core2.0预览版。
   通过对.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 根据数据库生成实体类的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product