• Entity层
 using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using System.Text; namespace Entity.Core
{
/// <summary>
/// DB表基础属性
/// </summary>
public abstract class BaseEntity<T>
{
public BaseEntity()
{
CreteTime = DateTime.Now;
}
/// <summary>
/// 主键Id
/// </summary>
[DataMember]
[Key]
public T Id { get; set; } /// <summary>
/// DB版号,Mysql详情参考;http://www.cnblogs.com/shanyou/p/6241612.html
/// </summary>
//[Timestamp]//Mysql不允许byte[]类型上标记TimeStamp/RowVersion,这里使用DateTime类型配合标记ConcurrencyCheck达到并发控制
[ConcurrencyCheck]
public DateTime RowVersion { get; set; } /// <summary>
/// 创建时间
/// </summary>
public DateTime CreteTime { get; set; }
}
}

BaseEntity

 using Entity.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text; namespace Entity.Table
{
/// <summary>
/// 商品类
/// </summary>
public class Product : BaseEntity<long>
{
/// <summary>
/// 名称
/// </summary>
[StringLength()]
[Required]
public string Name { get; set; } /// <summary>
/// 描述
/// </summary>
[StringLength()]
[Required]
public string Description { get; set; } /// <summary>
/// 类别
/// </summary>
[Range(, int.MaxValue)]
public int Category { get; set; } /// <summary>
/// 原价
/// </summary>
[Required]
public decimal Price { get; set; } /// <summary>
/// 现价
/// </summary>
public decimal Discount { get; set; }
}
}

Product

  • 在DAL层添加以下引用

Microsoft.EntityFrameworkCore

Microsoft.EntityFrameworkCore.Tools

Microsoft.EntityFrameworkCore.Relational

Pomelo.EntityFrameworkCore.MySql

 using Entity;
using Entity.Table;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DAL
{
public class ProductContext : DbContext
{
//https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/complex-data-model
public ProductContext(DbContextOptions<ProductContext> options) : base(options)
{
//在此可对数据库连接字符串做加解密操作
} public DbSet<Person> Person { get; set; }
public DbSet<Product> Product { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}

DbContext

  • Service 层

添加引用 Microsoft.EntityFrameworkCore.UnitOfWork

 using System;
using System.Collections.Generic;
using System.Text; namespace Service.ProductService
{
public interface IProductService
{
string Test();
}
}

IProductService

 using Entity.Table;
using Microsoft.EntityFrameworkCore; namespace Service.ProductService
{
public class ProductService : IProductService
{
private readonly IUnitOfWork _unitOfWork;
public ProductService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
} public string Test()
{
var repo = _unitOfWork.GetRepository<Product>();
repo.Insert(new Product
{
Category = ,
Description = "此商品为澳洲代购,买不了吃亏买不了上当",
Discount = (decimal)899.21,
Price = (decimal)98.2,
Name = "澳洲袋鼠粉",
});
_unitOfWork.SaveChanges();//提交到数据库
var result = repo.GetFirstOrDefault()?.Description ?? string.Empty;
return result;
}
}
}

ProductService

  • appsettings.json 的配置
{
"ConnectionStrings": {
"MySqlConnection": "Server=localhost;database=ProjectManager;uid=root;pwd=password;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
  • Startup 的配置
        public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ProductContext>(options =>
options.UseMySql(Configuration.GetConnectionString("MySqlConnection")));//添加Mysql支持 using Microsoft.EnityFrameworkCore services.AddUnitOfWork<ProductContext>();//添加UnitOfWork支持
services.AddScoped(typeof(IProductService), typeof(ProductService));//用ASP.NET Core自带依赖注入(DI)注入使用的类 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
  • 有关数据库迁移命令 (注意要选择保护DbContext项目, 视图 -> 其它窗口 ->  程序包管理器控制台)

add-migration init  (注: 初始化)

update-database

add-migration changeProductTable

remove-migration

如果出现找不到表  __efmigrationshistory, 则运行以下SQL

CREATE TABLE `__EFMigrationsHistory` (
`MigrationId` varchar(95) NOT NULL,
`ProductVersion` varchar(32) NOT NULL,
PRIMARY KEY (`MigrationId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

创建 __EFMigrationsHistory 表

ef.core Mysql的更多相关文章

  1. EF Core MYSQL 生成表映射配置问题

    Model表 public class Goods { public string ID { get; set; } public string CreatedBy { get; set; } pub ...

  2. EF Core MySql GUID配置方式

    builder.Property(m => m.Id) .HasColumnName("Id") .ForMySQLHasColumnType("char(36)& ...

  3. EF Core 日志跟踪sql语句

    EF Core 日志跟踪sql语句 官方文档链接:https://docs.microsoft.com/en-us/ef/core/miscellaneous/logging 1.新增自定义ILogg ...

  4. .Net EF Core千万级数据实践

    .Net 开发中操作数据库EF一直是我的首选,工作和学习也一直在使用.EF一定程度的提高了开发速度,开发人员专注业务,不用编写sql.方便的同时也产生了一直被人诟病的一个问题性能低下. EF Core ...

  5. Asp.net Core 通过 Ef Core 访问、管理Mysql

    本文地址:http://www.cnblogs.com/likeli/p/5910524.html 环境 dotnet Core版本:1.0.0-preview2-003131 本文分为Window环 ...

  6. MySQL官方.NET Core驱动已出,支持EF Core

    千呼万唤始出来MySQL官方.NET Core驱动已出,支持EF Core. 昨天MySQL官方已经发布了.NET Core 驱动,目前还是预览版,不过功能已经可用. NuGet 地址:https:/ ...

  7. net Core 通过 Ef Core 访问、管理Mysql

    net Core 通过 Ef Core 访问.管理Mysql 本文地址:http://www.cnblogs.com/likeli/p/5910524.html 环境 dotnet Core版本:1. ...

  8. asp.net core + mysql + ef core + linux

    asp.net core + mysql + ef core + linux 以前开发网站是针对windows平台,在iis上部署.由于这次需求的目标服务器是linux系统,就尝试用跨平台的.NET ...

  9. EF Core下利用Mysql进行数据存储在并发访问下的数据同步问题

    小故事 在开始讲这篇文章之前,我们来说一个小故事,纯素虚构(真实的存钱逻辑并非如此) 小刘发工资后,赶忙拿着现金去银行,准备把钱存起来,而与此同时,小刘的老婆刘嫂知道小刘的品性,知道他发工资的日子,也 ...

随机推荐

  1. spark基础知识(1)

    一.大数据架构 并发计算: 并行计算: 很少会说并发计算,一般都是说并行计算,但是并行计算用的是并发技术.并发更偏向于底层.并发通常指的是单机上的并发运行,通过多线程来实现.而并行计算的范围更广,他是 ...

  2. mysql定时器设置开机默认自启动

    1).查询mysql安装位置:show variables like "%char%"; 2).查询定时器是否开启: -查询定时器状态:show VARIABLES LIKE '% ...

  3. 谷歌浏览器安装json格式化插件

    1.下载JsonView扩展程序压缩包 下载地址:https://github.com/gildas-lormeau/JSONView-for-Chrome 点击[Clone or download] ...

  4. wireshark 抓包过滤器使用

    目录 wireshark 抓包过滤器 一.抓包过滤器 二.显示过滤器 整理自陈鑫杰老师的wireshark教程课 wireshark 抓包过滤器 过滤器分为抓包过滤器和显示过滤器,抓包过滤器会将不满足 ...

  5. 清北学堂2019NOIP提高储备营DAY1

    今天是第二次培训的第一天,关于NOIP的基础算法,主要内容如下: $1.枚举 $2.搜索 $3.贪心 $1.枚举: •定义: 枚举又叫做穷举,是一种基础的算法,其思路主要是:从问题中有可能的解集中一一 ...

  6. android TextView加载html 过滤所有标签,保留换行标签

    情景: TextView加载后端接口获取到的html富文本 遇到的问题: 客户端通过接口取到的数据如下: <p style="margin-top: 0px; margin-botto ...

  7. Android应用市场的帮助类

    写了一个Android应用市场的帮助类,如下: public class MarketUtils { public static final String MARKET_DATA = "ma ...

  8. 使用SecureCRT做端口转发

    我的笔记本只能访问跳板机,跳板机是Linux系统,访问内网机器需要在跳板机内通过ssh命令访问,特别不方便,而且我们还需要访问Windows或web网站. 这是我们就可以做一个端口转发,通过自己的笔记 ...

  9. C++示例

    Linux C++ template使用示例: #include <iostream> #include <cstring> using namespace std; temp ...

  10. 用Mysql进行emp、dept、salgrade表的相关查询操作

    初学者都会接触到三种表:emp.dept.salgrade表,进行练习各种语句操作再合适不过 但是,网上大多数的操作语句都是用oracle进行操作的,小编在学习mysql的时候,参考网上的书写遇到了不 ...