efcore8.0 mysql 生成sql执行时生成sql语句

//nuget包

 <ItemGroup>
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
        <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
        <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
    </ItemGroup>

//数据库上下文

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace mysql_test;

[Table("tb_point")]
public class Point
{
    [Key]
    public string Uid { get; set; }
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }
}

public class MyDbContext: DbContext
{
    private static ILoggerFactory loggerFactory = LoggerFactory.Create(b=>b.AddConsole());
    public DbSet<Point> Points { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseMySql("server=localhost;port=3306;database=mydb;user=root;password=peng@123;", new MariaDbServerVersion(new Version(8, 0, 27)));
        optionsBuilder.UseLoggerFactory(loggerFactory);
    }
}

//主方法

using System.Diagnostics;

namespace mysql_test;

class Program
{
    static void Main(string[] args)
    {
       // Insert();//生成insert语句
        Update( Query());//生成update语句
        Console.WriteLine("Hello, World!");
    }
    static List<Point> GeneratePoints(int count)
    {
        Random random = new Random();
        List<Point> points = new List<Point>();

        for (int i = 0; i < count; i++)
        {
            Point point = new Point
            {
                Uid = Guid.NewGuid().ToString(),
                X = random.NextDouble(),
                Y = random.NextDouble(),
                Z = random.NextDouble()
            };

            points.Add(point);
        }

        return points;
    }
    private static void Insert()
    {
        using (var context = new MyDbContext())
        {
           //使用的是Code first 模式。当数据库不存在是,自动构建数据库和表结构
            if (!context.Database.CanConnect())
            {
                context.Database.EnsureCreated();
            }

            List<Point> points = GeneratePoints(10000);

            context.Points.AddRange(points);
            context.SaveChanges();
        }
    }
    private static List<Point> Query()
    {
        using (var context = new MyDbContext())
        {
            return context.Points.Take(10000).ToList();
        }
    }
    static void RefreshPoints(List<Point> points)
    {
        Trace.WriteLine(points.Count.ToString());
        Random random = new Random();

        foreach (var point in points)
        {
            // 模拟刷新数据,这里使用随机数
            point.X = random.NextDouble();
            point.Y = random.NextDouble();
            point.Z = random.NextDouble();
        }
    }
    private static void Update(List<Point> points)
    {
        RefreshPoints(points);
        Stopwatch st = new Stopwatch();
        st.Start();
        using (var context = new MyDbContext())
        {
            context.Points.UpdateRange(points);
            context.SaveChanges();
        }
        st.Stop();
        Trace.WriteLine(st.ElapsedMilliseconds.ToString());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值