• Vs2017 NetCode EF Mysql 控制台应用


    1  运行环境   vs2017   NetCode2.0   

    2 NuGet  MySql.Data.EntityFrameworkCore 8.0.18   Json.Net 1.0.16

    3  源代码 https://github.com/chxl800/EFMysqlDemo

    1. 项目创建流程视图

    1. 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));
     
        }
    }
    1. 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; }
        }
    }
    1. 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();
            }
        }
    
    
    
    }
    1. 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; }
        }
    }
    1. 运行结果

  • 相关阅读:
    14.3
    14.2
    14.1
    第14章 抽象类和接口
    13.5
    JAVA异常处理
    12.9
    12.7
    vs 常用快捷键
    click()和onclick()的区别
  • 原文地址:https://www.cnblogs.com/chxl800/p/11725191.html
Copyright © 2020-2023  润新知