EFCore学习记录笔记

1:连接slqlocaldb数据库

(1)在CMD下可以输入sqllocaldb info 查看本机安装的所有的localdb实例

(2)数据库连接字符串为:“Server=(localdb)\MSSQLLocalDB;Database=JiangEFCoreDemo;integrated security=True;“

2:使用EFCore需要下载NuGet包Microsoft.EntityFrameworkCore.SqlServer,

  使用迁移命令还需要下载Microsoft.EntityFrameworkCore.Tools

在VS中打开“程序包管理控制台” 输入get-help entityframeworkcore 可以查询迁移使用的命令

输入 Add-Migration  起个名字(一般起inital)  //会生成迁移文件,文件名带有时间戳

输入Script-Migration 回车 //生成sql脚本文件,可以提交给DBA管理员让他帮助执行

输入Update-Database 回车//直接在数据库中生成相应的操作,一般用于开发环境

3:输出Sql语句到控制台

 

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .AddFilter("Microsoft.EntityFrameworkCore.Database.Command[20101]", LogLevel.Information)
                .AddConsole()
                .AddDebug()
                    .AddEventLog();
            });
            services.AddDbContext<MyContext>(
                  options =>
                  {
                      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));

                      options.UseLoggerFactory(loggerFactory);


                  }
                );
        }
原文地址:https://www.cnblogs.com/jiangyunfeng/p/11978776.html