标签:virtual config mysql 消息 ast soft 数据 客服 services
此文仅是对于netcore基于mysql的简单的codefirst实现的简单记录。示例为客服系统消息模板的增删改查实现
须引入Pomelo.EntityFrameworkCore.MySql和Microsoft.EntityFrameworkCore
/// <summary> /// 消息模板 /// </summary> public class MessatgeTemplate { /// <summary> /// id /// </summary> [Key] public System.Guid Id { get; set; } /// <summary> /// 名称 /// </summary> [MaxLength(250)] public string Name { get; set; } /// <summary> /// 关键字 /// </summary> [MaxLength(500)] public string Keywords { get; set; } /// <summary> /// 内容 /// </summary> public string Content { get; set; } /// <summary> /// 语言 /// </summary> [MaxLength(80)] public string CultureCode { get; set; } /// <summary> /// 类别 /// </summary> [MaxLength(250)] public string Category { get; set; } /// <summary> /// 备注信息 /// </summary> public string Remark { get; set; } /// <summary> /// 是否启用 /// </summary> public bool Enabled { get; set; } /// <summary> /// 创建时间 /// </summary> public DateTime Created { get; set; } /// <summary> /// 最近修改时间 /// </summary> public DateTime LastModified { get; set; } }
/// <summary> /// /// </summary> public class CustomerServiceContext : DbContext { public CustomerServiceContext(DbContextOptions<CustomerServiceContext> options) : base(options) { } /// <summary> /// 配置 /// </summary> /// <param name="optionsBuilder"></param> protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); } public virtual DbSet<MessatgeTemplate> MessatgeTemplate { get; set; } }
/// <summary> /// 客服业务统一接口 /// </summary> public interface ICustomerServicerSerice { }
/// <summary> /// 模板业务类 /// </summary> public class MessageTemplateService : ICustomerServicerSerice { private CustomerServiceContext _CustomerServiceContext; public MessageTemplateService(CustomerServiceContext customerServiceContext) { this._CustomerServiceContext = customerServiceContext; } /// <summary> /// 创建模板 /// </summary> public void CreateMessageTemplate() { //todo create } /// <summary> /// 修改模板 /// </summary> public void ModifyMessageTemplate() { //TODO modified } /// <summary> /// 获取模板 /// </summary> /// <returns></returns> public List<object> GetMessageTemplate() { //TODO根据传入参数查询模板 return null; } /// <summary> /// 删除 /// </summary> public void DeleteMessageTemplate(Guid id) { var template = _CustomerServiceContext.MessatgeTemplate.FirstOrDefault(p => p.Id == id); if (template != null) { _CustomerServiceContext.MessatgeTemplate.Remove(template); _CustomerServiceContext.SaveChanges(); } } /// <summary> /// 批量设置是否可用 /// </summary> /// <param name="ids"></param> /// <param name="enabled"></param> public void BatchEnableTemplate(List<Guid> ids, bool enabled) { //TODO set enabled } }
须引入Microsoft.EntityFrameworkCore和Microsoft.EntityFrameworkCore.Design.
1、在appsettings.json中配置数据连接
"ConnectionStrings": { "CumstomerServiceConnection": "server=localhost;port=3306;database=CustomerServiceCenter;uid=root;pwd=root23456;CharSet=utf8" }
2、修改startup.cs类的ConfigureServices注入db访问类
services.AddDbContext<CustomerServiceContext>(p => p.UseMySql(Configuration.GetConnectionString("CumstomerServiceConnection")));
3、设置该webapi项目为启动项目,在nuget程序包控制台选择第一步中创建的实体类项目依次运行如下脚本创建数据库
1)add-migration init
2) update-database
上面的两个命令中,add-migration表示添加数据迁移,其实就是将实体变更转换成sql(add-migraion后面的init只是个名称,可以自行定义),update-database是将生成的数据迁移信息更新到数据库
另外一些可能用到的命令
remove-migration ---将最近生成的迁移(没有更新到数据库的)移除掉
script-migration --生成数据库迁移相应的脚本
------------------------
命令执行成功后,就可以在数据库中找到创建好的数据库和表了
script-datatable可以生成数据库脚本语句
好了,以上就完成了codefirst的简单操作了。不过跟着第二步的业务类其实没啥关系!!!如果只是进行数据迁移确实没有service层什么事,但是业务逻辑要走通,实现crud,业务层还是必须的。
---------------------------------------------------------------------------------------------------------------------
在webapi创建MessageTemplateController
[Route("api/[controller]")] [ApiController] public class MessageTemplateController : ControllerBase { private MessageTemplateService _MessageTemplateService; public MessageTemplateController(MessageTemplateService messageTemplateService) { this._MessageTemplateService = messageTemplateService; } /// <summary> /// 删除 /// </summary> /// <param name="id"></param> [HttpPost("DeleteMessageTemplate")] public void DeleteMessageTemplate(Guid id) { _MessageTemplateService.DeleteMessageTemplate(id); } //TODO其他操作:查询,新增,修改等 }
修改Startup.cs类,注入服务
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddDbContext<CustomerServiceContext>(p => p.UseMySql(Configuration.GetConnectionString("CumstomerServiceConnection"))); #region 注入业务服务 Assembly assembly = Assembly.Load("Qingy.DotNetCoreStudy.CustomerServiceService"); List<Type> types = assembly.GetTypes().Where(u => u.IsClass && !u.IsAbstract && !u.IsGenericType && u.GetInterfaces().Any(p => p == typeof(ICustomerServicerSerice)) ).ToList(); foreach (var item in types) { services.AddTransient(item); } #endregion }
综合以上,就可以将api到db的完整实现了。
标签:virtual config mysql 消息 ast soft 数据 客服 services
原文地址:https://www.cnblogs.com/johnyong/p/12840098.html