//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());
}
}