ef.core Mysql
- 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的更多相关文章
- EF Core MYSQL 生成表映射配置问题
Model表 public class Goods { public string ID { get; set; } public string CreatedBy { get; set; } pub ...
- EF Core MySql GUID配置方式
builder.Property(m => m.Id) .HasColumnName("Id") .ForMySQLHasColumnType("char(36)& ...
- EF Core 日志跟踪sql语句
EF Core 日志跟踪sql语句 官方文档链接:https://docs.microsoft.com/en-us/ef/core/miscellaneous/logging 1.新增自定义ILogg ...
- .Net EF Core千万级数据实践
.Net 开发中操作数据库EF一直是我的首选,工作和学习也一直在使用.EF一定程度的提高了开发速度,开发人员专注业务,不用编写sql.方便的同时也产生了一直被人诟病的一个问题性能低下. EF Core ...
- Asp.net Core 通过 Ef Core 访问、管理Mysql
本文地址:http://www.cnblogs.com/likeli/p/5910524.html 环境 dotnet Core版本:1.0.0-preview2-003131 本文分为Window环 ...
- MySQL官方.NET Core驱动已出,支持EF Core
千呼万唤始出来MySQL官方.NET Core驱动已出,支持EF Core. 昨天MySQL官方已经发布了.NET Core 驱动,目前还是预览版,不过功能已经可用. NuGet 地址:https:/ ...
- net Core 通过 Ef Core 访问、管理Mysql
net Core 通过 Ef Core 访问.管理Mysql 本文地址:http://www.cnblogs.com/likeli/p/5910524.html 环境 dotnet Core版本:1. ...
- asp.net core + mysql + ef core + linux
asp.net core + mysql + ef core + linux 以前开发网站是针对windows平台,在iis上部署.由于这次需求的目标服务器是linux系统,就尝试用跨平台的.NET ...
- EF Core下利用Mysql进行数据存储在并发访问下的数据同步问题
小故事 在开始讲这篇文章之前,我们来说一个小故事,纯素虚构(真实的存钱逻辑并非如此) 小刘发工资后,赶忙拿着现金去银行,准备把钱存起来,而与此同时,小刘的老婆刘嫂知道小刘的品性,知道他发工资的日子,也 ...
随机推荐
- Ubuntu通过ADB连接手机
参考 ubuntu14.04 下android studio连接手机 安装 adb $sudo apt install adb $sudo lsusb 得到ID为 Bus Device : ID 12 ...
- 【5】学习C++之类的概念
C++ 中的类(Class)可以看做C语言中结构体(Struct)的升级版.结构体是一种构造类型,可以包含若干成员变量,每个成员变量的类型可以不同:可以通过结构体来定义结构体变量,每个变量拥有相同的性 ...
- java 中final关键字
1.final变量,一旦该变量被设定,就不可以再改变该变量的值. final关键字定义的变量必须声明时赋值.一旦一个对象引用被修饰为final后,它只能恒定指向一个对象,一个既是static和fina ...
- 题解 洛谷P3936 Coloring
考虑搜索,发现复杂度爆炸 贪心,正确性过低(~~实测爆炸~~) 于是,~~发现~~这题是模拟退火 这里不讲解退火的定义了,初学退火可以去平衡点 退火本身维护一个答案图像,答案的q,当前图 ...
- Ansible-----include
什么是include 在ansible中,我们可以通过include,在一个playbook中包含另一个文件,以便实现代码的重复利用. include_tasks模块 include_tasks模块用 ...
- [Kubernetes]基于角色的权限控制之RBAC
Kubernetes中有很多种内置的编排对象,此外还可以自定义API资源类型和控制器的编写方式.那么,我能不能自己写一个编排对象呢?答案是肯定的.而这,也正是Kubernetes项目最具吸引力的地方. ...
- centos命令安装
1.解决ifconfig命令失效:需要安装net-tools工具 yum install net-tools 2.免密码登录 (1)通过命令,产生公钥信息 ssh-keygen -t rsa 如果提示 ...
- LoadRunner场景设置
创建负载测试场景场景目标:模拟10家旅行社同时登陆.搜索航班.购买机票.查看航班路线并退出打开Controller并创建一个新场景1.打开HP LoadRunner2.打开Controller在Loa ...
- java 异步线程下的顺序控制
转载请注明出处!!!! java.util.concurrent.CountDownLatch 的使用可以达到效果 CountDownLatch是JAVA提供在java.util.concurre ...
- mongodb将时间作为条件查询
db.stock_signal_single.find({"createtime": { "$lt" : ISODate("2019-04-08T00 ...