青春年华

.net core 引进实体

电脑版发表于:2019/12/23 10:47

 需要使用工具的Nuget包管理器添加EF的依赖。


Install-Package Microsoft.EntityFrameworkCore.SqlServer


如果是使用db first,需要根据数据库生成model,就还需要使用命令添加两个依赖

Install-Package Microsoft.EntityFrameworkCore.Tools    Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design

相关依赖注入成功之后,就可以再根据一个命令从数据库生成model了

Scaffold-DbContext "Server=.;Database=HouseDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

  注意:这里的Database=数据库的名称

if版本太低,

看下操作:

链接网页看详细介绍。

 https://www.cnblogs.com/tshaoguo/p/10142743.html

然后我们再次使用命令·即可:

 Scaffold-DbContext "Server=.;Database=HouseDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

生成效果如图所示:

public partial class Blog
    {
        public Blog()
        {
            Post = new HashSet<Post>();
        }
        public int BlogId { get; set; }
        public string Url { get; set; }
        public ICollection<Post> Post { get; set; }
    }
  public partial class Post
    {
        public int PostId { get; set; }
        public int BlogId { get; set; }
        public string Content { get; set; }
        public string Title { get; set; }
        public Blog Blog { get; set; }
    }




关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢