1 运行环境 vs2017 NetCode2.0
2 NuGet MySql.Data.EntityFrameworkCore 8.0.18 Json.Net 1.0.16
-
项目创建流程视图
-
JsonHelper.cs 时间格式 使用Json.Net
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using Json.Net;
namespace EFMysqlNetCode.Common
{
public static partial class JsonHelper
{
public static JsonConverter<DateTime> dateConverter = new JsonConverter<DateTime>(
dt => dt.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
s => DateTime.ParseExact(s, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
}
}
-
DBEntities.cs 数据库上下文
using EFMysqlNetCode.Model;
using Microsoft.EntityFrameworkCore;
namespace EFMysqlNetCode
{
public class DBEntities : DbContext
{
//这里也可以
string str = @"Data Source=localhost;Database=demodb;User ID=root;Password=root;pooling=true;CharSet=utf8;port=3306;sslmode=none";
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder.UseMySQL(str);
public DbSet<User> User { get; set; }
}
}
-
Program.cs 测试方法
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EFMysqlNetCode.Common;
using EFMysqlNetCode.Model;
using Json.Net;
namespace EFMysqlNetCode
{
class Program
{
static void Main(string[] args)
{
DBEntities db = new DBEntities();
//新增
User user = new User();
user.Id = Guid.NewGuid().ToString().Replace("-", "");
db.User.Add(user);
db.SaveChanges();
//查询
List<User> list= db.User.ToList();
//json转化
var userJson = JsonNet.Serialize(list, JsonHelper.dateConverter);
//var userList = JsonNet.Deserialize<List<User>>(userJson, JsonHelper.dateConverter);
Console.WriteLine(userJson);
Console.Read();
}
}
}
-
User.cs 实体类
using System;
namespace EFMysqlNetCode.Model
{
/// <summary>
/// 账号表
/// </summary>
public class User
{
/// <summary>
///
/// </summary>
public string Id { get; set; }
/// <summary>
///
/// </summary>
public string TenantId { get; set; }
/// <summary>
///
/// </summary>
public string UserName { get; set; }
/// <summary>
///
/// </summary>
public string RealName { get; set; }
/// <summary>
///
/// </summary>
public string UserCode { get; set; }
/// <summary>
///
/// </summary>
public string Password { get; set; }
/// <summary>
///
/// </summary>
public string Salt { get; set; }
/// <summary>
///
/// </summary>
public string Mobile { get; set; }
/// <summary>
///
/// </summary>
public string Email { get; set; }
/// <summary>
///
/// </summary>
public int UserType { get; set; }
/// <summary>
///
/// </summary>
public int Status { get; set; }
/// <summary>
///
/// </summary>
public string Creator { get; set; }
/// <summary>
///
/// </summary>
public DateTime CreateTime { get; set; }
/// <summary>
///
/// </summary>
public string Reviser { get; set; }
/// <summary>
///
/// </summary>
public DateTime ReviseTime { get; set; }
/// <summary>
///
/// </summary>
public DateTime? LoginTime { get; set; }
/// <summary>
///
/// </summary>
public string IP { get; set; }
/// <summary>
///
/// </summary>
public DateTime? LastLoginTime { get; set; }
/// <summary>
///
/// </summary>
public string LastIP { get; set; }
}
}
-
运行结果