EF Core 使用以及常用命令

 使用步骤

 

1. 新建一个控制台项目,在Visual Studio 中打开 程序包管理控制台,输入:

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

 

2. 在 Sql Server 中建立数据库和表

3. 在控制台项目中新建一个Models文件夹

4. 在程序包管理控制台输入:

Scaffold-DbContext "数据库连接字符串" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force -UseDatabaseNames

 

5. 在控制台中使用自动生成的 DbContext

using EFPlus.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
 
namespace EFPlus
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            //GetCount();
            await MultiDeleteWithSql();
 
            stopwatch.Stop();  //停止Stopwatch
            Console.WriteLine("Using Elapsed output runTime:{0}", stopwatch.Elapsed.ToString());//这里使用时间差来输出,如:时:分:秒
 
            Console.WriteLine("Hello World!");
        }
 
        //static void Main(string[] args)
        //{
        //    Stopwatch stopwatch = new Stopwatch();
        //    stopwatch.Start();
 
        //    MultiDelete();
 
        //    stopwatch.Stop();  //停止Stopwatch
        //    Console.WriteLine("Using Elapsed output runTime:{0}", stopwatch.Elapsed.ToString());//这里使用时间差来输出,如:时:分:秒
 
        //    Console.WriteLine("Hello World!");
        //}
 
 
        static void GetCount()
        {
            using (var db = new BlogDbContext())
            {
                var allblogs = db.Blogs.Count();
                Console.WriteLine(allblogs);
            }
 
        }
 
        static void MultiDelete()
        {
            using (var db = new BlogDbContext())
            {
                var allblogs = db.Blogs.ToArrayAsync();
                db.Blogs.RemoveRange(allblogs.Result);
                db.SaveChanges();
            }
            
        }
 
        static async Task MultiDeleteAsync()
        {
            using (var db = new BlogDbContext())
            {
                var allblogs = db.Blogs.ToArrayAsync();
                db.Blogs.RemoveRange(allblogs.Result);
                await db.SaveChangesAsync();
            }
        }
 
        /// <summary>
        /// 批量插入
        /// </summary>
        static void MultiInsert()
        {
            using (var db = new BlogDbContext())
            {
                for (int i = 0; i < 1000000; i++)
                {
                    var blog = new Blog { Url = "https://www.cnblog.com/p/" + i.ToString() };
                    db.Blogs.Add(blog);
                }
                db.SaveChanges();
            }
 
        }
 
        static async Task MultiInsertAsync()
        {
            using (var db = new BlogDbContext())
            {
 
                for (int i = 0; i < 1000000; i++)
                {
                    var blog = new Blog { Url = "https://www.cnblog.com/p/" + i.ToString() };
                    db.Blogs.Add(blog);
                }
                await db.SaveChangesAsync();
            }
        }
 
        static async Task MultiDeleteWithSql()
        {
            var db = new BlogDbContext();
            var conn = db.Database.GetDbConnection();
            try
            {
                await conn.OpenAsync();
                using (var command = conn.CreateCommand())
                {
                    string query = "delete from blogs where Id<1000001";
                    command.CommandText = query;
                    var rows = await command.ExecuteNonQueryAsync();
                }
            }
            finally
            {
                conn.Close();
            }
        }
 
    }
}

 

 

 

 

常用命令

增加迁移

Nuget 控制台

Add-Migration Initial -Context DefaultDbContext
Add-Migration Initial -Context MultiTenantDbContext

命令行

dotnet ef migrations add Initial --context DefaultDbContext
dotnet ef migrations add Initial --context MultiTenantDbContext

 

应用迁移

Nuget 控制台

update-database -context DefaultDbContext
update-database -context MultiTenantDbContext

命令行

dotnet ef database update --context DefaultDbContext
dotnet ef database update --context MultiTenantDbContext

 

删除迁移

Nuget 控制台

Remove-Migration -Context DefaultDbContext 
Remove-Migration -Context MultiTenantDbContext

命令行

dotnet ef migrations remove --context DefaultDbContext
dotnet ef migrations remove --context MultiTenantDbContext

 

删除整个数据库

Nuget 控制台

Drop-Database -Context DefaultDbContext -WhatIf
Drop-Database -Context MultiTenantDbContext -WhatIf

命令行

dotnet ef database drop --context DefaultDbContext
dotnet ef database drop --context MultiTenantDbContext

 

 

从数据库生成实体类

Nuget 控制台

Scaffold-DbContext "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=std_hse;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force -UseDatabaseNames

 

 

参考资料:

https://blog.csdn.net/xingkongtianyuzhao/article/details/102905185

Scaffold-DbContext 命令使用 - 小七要走 - 博客园 (cnblogs.com)

 

posted @ 2022-03-04 12:32  小丸  阅读(289)  评论(0编辑  收藏  举报