1 NetCode EF整合 代码

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

1.1 项目文件结构

1.2 NuGet  MySql.Data.EntityFrameworkCore 8.0.18

1.3 appsettings.json   增加数据库字符串链接ConnectionStrings

{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": { "MysqlConnection": "Data Source=localhost;Database=demodb;User ID=root;Password=root;pooling=true;CharSet=utf8;port=3306;sslmode=none" }
}

1.4 DBEntities.cs 添加数据库上下文

using EFMysqlNetCodeMvc.Models;
using Microsoft.EntityFrameworkCore; namespace EFMysqlNetCodeMvc
{
public class DBEntities : DbContext
{
public DBEntities(DbContextOptions<DBEntities> options) : base(options)
{
} ////这里也可以
//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.5 User.cs 增加数据库表实体类

1.6 Startup.cs 配置重点 有中文注释的新加的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFMysqlNetCodeMvc;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Serialization; namespace EFMysqlNetCodeMvc
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//json格式化
services.AddMvc()
.AddJsonOptions(options =>
{
//忽略循环引用
//options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //设置序列化时key为驼峰样式,开头字母小写输出 controller调用Josn(对象)
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//原样输出
//options.SerializerSettings.ContractResolver = new DefaultContractResolver(); //时间格式
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; //空值的字段不显示
//options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
}); //ef mysql 配置IOC
services.AddDbContext<DBEntities>(options => options.UseMySQL(Configuration.GetConnectionString("MySqlConnection")));
services.AddMvc();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
} app.UseStaticFiles(); // 跨域策略
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials()); //app.UseMvc();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}

1.7 HomeController.cs   mvc 代码用于测试

public class HomeController : Controller{

DBEntities db; 

public HomeController(DBEntities db)
{
     this.db = db;
}

      public IActionResult Test()
{ //查询
List<User> list = db.User.ToList(); //json序列化
//var userJson = JsonConvert.SerializeObject(list);
//var userList = JsonConvert.DeserializeObject<List<User>>(userJson); return Json(list);
} public IActionResult Add([FromBody] User user)
{
//新增
//User user = new User();
//user.Id = Guid.NewGuid().ToString().Replace("-", "");
//db.User.Add(user);
//db.SaveChanges(); return Json(user);
}
}

1.8 运行结果

1.9 重点代码说明

1.9.1 URL:/home/test   实体User.cs首字母大写  输出首字母变小写

            //json格式化
services.AddMvc()
.AddJsonOptions(options =>
{
//忽略循环引用
//options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //设置序列化时key为驼峰样式,开头字母小写输出 controller调用Josn(对象)
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//原样输出
//options.SerializerSettings.ContractResolver = new DefaultContractResolver(); //时间格式
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//空值的字段不显示
//options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});

1.9.2 URL:/home/add   输入参数首字母小写  自动转化为实体User.cs首字母大写

public IActionResult Add([FromBody] User user)

2.0 User.cs

using System;

namespace EFMysqlNetCodeMvc.Models
{
/// <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; }
}
}

Vs2017 NetCode Mvc EF Mysql 整合2的更多相关文章

  1. Vs2017 NetCode Mvc EF Mysql 整合1

    1  运行环境   vs2017   NetCode2.0 2 NuGet  MySql.Data.EntityFrameworkCore 8.0.18 3  源代码 https://github.c ...

  2. VS2017+EF+Mysql生成实体数据模型(解决闪退的坑) 版本对应才行

    最近要使用VS2017+EF+Mysql,在生成实体数据模型踏过一些坑,在此做个总结. 1.先下载并安装 mysql-connector-net-6.9.10.msi  和  mysql-for-vi ...

  3. VS2017 + EF + MySQL 我使用过程中遇到的坑

    原文:VS2017 + EF + MySQL 我使用过程中遇到的坑 写在前面: 第一次使用MySQL连接VS的时候本着最新版的应该就是最好的,在MySQL官网下载了最新版的MySQL没有并且安装完成之 ...

  4. VS2017+EF+Mysql生成实体数据模型(解决闪退的坑)

    原文:VS2017+EF+Mysql生成实体数据模型(解决闪退的坑) 最近要使用VS2017+EF+Mysql,在生成实体数据模型踏过一些坑,在此做个总结. 1.先下载并安装 mysql-connec ...

  5. spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)

    spring-3.1.2.cxf-3.1.3.mybaties.mysql 整合实现webservice需要的完整jar文件 地址:http://download.csdn.net/detail/xu ...

  6. Asp.Net MVC EF各版本区别

      2009年發行ASP.NET MVC 1.0版 2010年發行ASP.NET MVC 2.0版,VS2010 2011年發行ASP.NET MVC 3.0版+EF4,需要.Net4.0支持,VS2 ...

  7. [转帖]Asp.Net MVC EF各版本区别

    Asp.Net MVC EF各版本区别 https://www.cnblogs.com/liangxiaofeng/p/5840754.html 2009年發行ASP.NET MVC 1.0版 201 ...

  8. [转]Asp.Net MVC EF各版本区别

    本文转自:http://www.cnblogs.com/liangxiaofeng/p/5840754.html 2009年發行ASP.NET MVC 1.0版 2010年發行ASP.NET MVC ...

  9. vs2012 + web api + OData + EF + MYsql

    vs2012 + web api + OData + EF + MYsql 开发及部署 先说下我的情况,b/s开发这块已经很久没有搞了,什么web api .MVC.OData都只是听过,没有实际开发 ...

随机推荐

  1. mysql使用truncate截断带有外键的表时报错--解决方案

    报错内容如:1701 - Cannot truncate a table referenced in a foreign key constraint 一.为什么要使用truncate 使用trunc ...

  2. 使用一般处理程序生成 JSON

    在 .NET 3.5 之后,定义在命名空间 System.Runtime.Serialization.Json 中的 DataContractJsonSerializer 可以帮助我们直接将一个对象格 ...

  3. D-Link系列路由器漏洞挖掘

    参考 http://www.freebuf.com/articles/terminal/153176.html https://paper.seebug.org/429/ http://www.s3c ...

  4. Entitas--ECS框架插件

    ECS Entity.Component.System Entity Component System 模块解耦 守望先锋 https://gameinstitute.qq.com/community ...

  5. 【VS开发】【CUDA开发】如何在MFC中调用CUDA

    如何在MFC中调用CUDA 有时候,我们需要在比较大的项目中调用CUDA,这就涉及到MFC+CUDA的环境配置问题,以矩阵相乘为例,在MFC中调用CUDA程序.我们参考罗振东iylzd@163.com ...

  6. python 元组tuple - python基础入门(14)

    在上一篇文章中我们讲解了关于python列表List的相关内容,今天给大家解释一下列表List的兄弟 – 元组,俗称: tuple. 元组tuple和列表List类似,元组有如下特点: 1.由一个或者 ...

  7. (九)Javabean与Jsp(来自那些年的笔记)

    目录 JavaBean 在JSP中使用JavaBean 标签用法 带标签体的 JavaBean 标签 setProperty 标签 getProperty 标签 JSP开发模式 案列:使用 模式一 编 ...

  8. VC++实现遍历指定文件夹

    VC++实现遍历指定文件夹,并进行深度遍历,一级,二级...最终列出该文件夹下所有文件全路径. #include "stdafx.h" #include <iostream& ...

  9. 开源定时任务框架Quartz(二)

    概述 上一篇文章完成了第一个Quartz程序的编写,这篇从Quartz中的几个重要对象来更深一层认识Quartz框架. Job和JobDetail Job是Quartz中的一个接口,接口下只有exec ...

  10. MySQL之锁、事务、优化、OLAP、OLTP

    本节目录 一 锁的分类及特性 二 表级锁定(MyISAM举例) 三 行级锁定 四 查看死锁.解除锁 五 事务 六 慢日志.执行计划.sql优化 七 OLTP与OLAP的介绍和对比 八 关于autoco ...